0

Файл библиотеки

test2.cpp

#include <test2.h> 
int add(int a, int b){
    return a+b;
}

Заголовочный файл test2.h

int add(int, int);

файл проекта test.cpp

#include <iostream>
#include "lib/test2.h"
using namespace std;

int main(){ cout<<add(1,2)<<endl; return 0; }

Компилирую при помощи gcc test.bat

g++ -E  test.cpp  -o test.ii
g++ -S test.ii -o test.s
as test.s -o test.o
g++ test.o -o test

И мне выдаёт ошибку

C:\Users\User\Desktop\C++\test>g++ test.o -o test
C:/Strawberry/c/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: test.o:test.cpp:(.text+0x1f): undefined reference to `add(int, int)'
collect2.exe: error: ld returned 1 exit status

Расположение файлов введите сюда описание изображения

  • 1
    ¿Не смущает, что test2.cpp при сборке никак не используется? ¿Или что упомянутой статической библиотеки нет? – user7860670 Jun 04 '22 at 15:29
  • В чем прикол делать препроцессинг и компиляцию в ассемблер отдельными командами? Можно сразу g++ test.cpp -c -o test.o. – HolyBlackCat Jun 04 '22 at 15:46

0 Answers0