1

I have a C++ project which uses multiple external static libraries and a huge code base.

If I put it all together like this:

g++ (ALL INCLUDE PATHS) (FLAGS) (ALL LIBRARIES) (ALL SOURCES) -o FILE

All at once in the same command, it works.


However I want to use a Makefile to generate file objects and speed up the compiling process.

I am able to generate a object file for each individual source file, but when trying to put them all together like this:

g++ -o FILE (ALL OBJECT FILES FOUND RECURSIVELY IN THE PATH)

I get the following errors, as if they had not been compiled with the static libraries

Linux_Release/.compilation_libs/linux.o: In function `WndProc(int, int, int)':
main_Linux.cpp:(.text+0xc6): undefined reference to `XRefreshKeyboardMapping'
main_Linux.cpp:(.text+0x18c): undefined reference to `XLookupKeysym'
main_Linux.cpp:(.text+0x20b): undefined reference to `Xutf8LookupString'
...

I've tried to put the includes/flags/libraries back into the command once again, but it doesn't make a difference.

Here are the flags I'm using for compiling each source file individually:

CFLAGS := -fmessage-length=0 -std=c++11 -Wno-sign-compare -Wno-unused-local-typedefs -Wno-reorder -Wno-switch -fpermissive -static-libstdc++ -static-libgc c -Wl,-rpath=.

And here are some of the libs included in the command above

LIBS := -lcurl -lz -lX11 -lXi -lGL -lGLU -lGLEW -lfreetype -lbass -lbass_fx -lOpenCL (and more...)

And the target for building each file object looks like this:

$(COMPILATION_LIBS_FOLDER)/%.o: %.cpp
mkdir -p '$(@D)'
g++ $(CFLAGS) $(FIXED_INCLUDES) $(LIBPATH) $(LIBS) -c $< -o $@

resolritter
  • 331
  • 2
  • 7
  • The way you're described it, when compiling files separately you're not linking in the static libraries. It is necessary to link in the static libraries, preferably by listing them at the end of the command line (since that will ensure all object files can resolve calls into the library). It is not necessary to include libraries when compiling individually, but is when linking them together. – Peter Sep 05 '18 at 10:51
  • you need to include the libraries when creating the executable: `g++ your_object_files.o -o FILE $(LIBS)` – Mike van Dyke Sep 05 '18 at 10:52
  • 1
    passing linker flags to `g++ -c` does nothing. You need to specify them when you're linking everything together, as @MikevanDyke said. – Botje Sep 05 '18 at 11:10
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Alan Birtles Sep 05 '18 at 12:07
  • Thank you for your advice! Following your tips, I was able to compile them together after putting the linking options **at the end** of the command instead. And as @Botje noticed, the linker flags aren't useful for generating each individual file object, only the path includes; so I went ahead and removed that. Now it works. – resolritter Sep 06 '18 at 03:15

0 Answers0