Reputation: 311
First, Background info: Ubuntu 10.10, gcc 4.4.5, C++, Qt Creator 2.3.1, FLANN.
Problem: Every time I compile my code I get an error saying 'undefined reference to [function from FLANN]'.
Explanation: I've been working on a GUI in Qt Creator that will utilize a few classes I built that include references to the FLANN library (not necessarily that important to know). Everything was going smoothly until I incorporated these classes and their header files, of course. I added the library to the .pro file just in case, but that didn't solve my problem. I narrowed down the situation to how Qt is making the files as compared to how I was making the files when I was testing my classes with a Makefile:
My Makefile: g++ -g process_stuff.o driver.o -o test.exe /usr/local/lib/libflann_s.a
I'm appending this library to the end of the gcc command, and it works perfectly. Further, if I remove the /usr/local/lib/libflann_s.a I get the same error making it myself that I got out of Qt.
Question: What exactly am I doing by including the library at the end of my gcc calls, and what can I do to have this happen in Qt Creator? All help is appreciated, and thanks in advance.
Upvotes: 0
Views: 408
Reputation: 311
But moments later.... unix:!macx:!symbian: LIBS += -L$$PWD/../../../../usr/local/lib/ -lflann_cpp_s Upon fixing that to: unix:!macx:!symbian: LIBS += -L$$PWD/../../../../usr/local/lib/ -lflann_s
The Qt library wizard decided to include the wrong file as the library. After changing the file, everything works.
Upvotes: 0
Reputation: 2750
The short answer to what you're doing by adding the library at the end of the gcc command is telling the build system where it can find the library and what library you want to link into your code. The -L part indicates the path to libraries and the -l is the name of the library you wish to link against. Both of which I suspect are not being included in your Qt Project file, which is why it isn't building when you're running it normally. You can add the two in the project as:
INCLUDEPATH += $$quote(/usr/local/include)
LIBS += $$quote(-lflann_cpp_s) \
$$quote(-L/usr/local/lib/)
Upvotes: 2
Reputation: 2686
External libraries are usually provided in two forms: static libraries and shared libraries. Static libraries are the ‘.a’ files . When a program is linked against a static library, the machine code from the object files for any external functions used by the program is copied from the library into the final executable.
Upvotes: 1