Question

Im learning c++ and I compile from the command line. I have a problem when it comes to trying to add 3rd party libraries. I cant seam to figure out the linker system. Does anyone know a good tutorial or something like that?

For example I want to play around with the SDL2 library and ill use a command like this.

c++ -I/Library/Frameworks/SDL2.framework/Headers -L/Library/Frameworks/SDL2.framework/ -lSDL2 helloworld.cpp

and I get the error ld: library not found for -lSDL2

Was it helpful?

Solution 2

I found out the answer. The following command compiled correctly. The include statement had to be changed to...

#include<SDL2/SDL.h>
and the correct compile command is...
c++ -o helloworld helloWorld.cpp -framework SDL2

I could also have used g++. On my system both c++ and g++ are symlinks to the same gnu compiler which happens to be the latest version I have installed on the system.

the option -L is a unix linker option and does not work on a MAC. The dev's for GCC were kind enough to include MAC specific linker options in the form of -framework. These serve to follow the mac tradition of how and where they like to store libraries. You can link several frameworks together by separating them with a comma. So for example i could also do -framework SDL2,SDL2_mixer as long as my source has

#include<SDL2_mixer/SDL_mixer.h> 

When compiling this default search location for libraries is /Library/Frameworks. The include statement is cross platform compatable and the mac gnu linker knows that if I say

#include<SDL2/SDL.h> 

that that header will be found at /Library/Frameworks/SDL2.framework/Headers

The -IPATH option still works on mac and can be used to pass alternate search locations for header and source files just like it works in unix.

OTHER TIPS

You need to put the linking flags last on the line:

g++ -I/Library/Frameworks/SDL2.framework/Headers  helloworld.cpp -L/Library/Frameworks/SDL2.framework/ -lSDL2
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top