0

I've been staring on this for hours, but nothing (not even other questions on here) helps. I got a C++ library (unlike most other questions, which link C from C++!) which I'm trying to link from another C++ project.

The library (in lib/libjttp.a, as expected by ld) has a function void jtpp::equals(int t1, int t2, std::string msg), which I'm trying to call from the other project.

When linking using g++ -L./lib/ -ljtpp obj/ex1.o obj/preproc/ex1.m.o -o bin/ex1, the linker keeps throwing:

/usr/bin/ld: obj/ex1.o: in function `ex1()':
ex1.cpp:(.text+0x4d): undefined reference to `jtpp::equals(int, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: error: ld returned 1 exit status
make: *** [Makefile:17: bin/ex1] Error 1

I checked the ELF dump (using objdump | c++filt) and that method is included.

The library is built (from source) in the usual way (I executed these commands in another directory, then copied the resulting library):

g++ -Wall -c src/suite.cpp -Isrc/inc/ -o obj/suite.o
ar rcs lib/libjttp.a obj/suite.o

When I copy the suite.o object file to the other project and link using the object file and not the library (g++ obj/ex1.o obj/preproc/ex1.m.o suite.o -o bin/ex1), everything links and the executable is as expected.

What am I doing wrong when linking with the library?

Directory structure (in the new project):

 - obj/
    + preproc/
        ~ ex1.m.o
    + ex1.o
 - lib/
    + libjttp.a
Jonas
  • 35
  • 1
  • 6
  • You missed to specify that library using `-ljttp` or something similar. – πάντα ῥεῖ May 16 '21 at 10:04
  • @πάνταῥεῖ When linking using `g++ -L./lib/ -ljtpp obj/ex1.o obj/preproc/ex1.m.o -o bin/ex1`, the `-ljttp` is given. – Jonas May 16 '21 at 10:10
  • Apparently the issue was in the order of the arguments: always specify -l before the objects you're trying to link /facepalm – Jonas Jun 04 '21 at 14:35

0 Answers0