1

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

  • possible duplicate of [Linker order - GCC](http://stackoverflow.com/questions/45135/linker-order-gcc) – Carl Norum Dec 12 '13 at 00:55
  • 1
    Not sure about it being a duplicate, as that is not the problem reported, unless you can tell from the example given that that is in fact the problem, and if it is, you might help the user by pointing this out, and then maybe they can delete their question. – mydoghasworms Dec 12 '13 at 04:59
  • His question is specific to the order of multiple linked libraries im only trying to use one library. – citizen2191629 Dec 12 '13 at 15:23

2 Answers2

1

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
Joe Z
  • 17,413
  • 3
  • 28
  • 39
1

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.