Public Group

GNU C++ command line help needed

This topic is 1861 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Im new to game development and having trouble with getting my source code to compile and link to the SDL library. Im trying to compile the following simple code.

#include<SDL.h>

#include<iostream>

int main(){

std::cout << "Hello world.";

return 0;

}

Im on a Mac, OS X and the sdl library is installed @  /Library/Frameworks/SDL2.framework/

My command to compile is

but I get the error that the SDL2 library was not found. Does anyone know what im doing wrong?

Share on other sites

What is in the /Library/Frameworks/SDL2.framework/ directory? Have you tried using g++ instead of c++ on the command line? Perhaps another compiler is providing the c++ executable.

Share on other sites

yeah, does that directory take you directly to the header files.  My include directory points to a folder containing a folder called SDL2, so I use #include <SDL2/SDL.h>

Also I link to:  -lSDL2main -lSDL2

Share on other sites

1.

"What is in the /Library/Frameworks/SDL2.framework/ directory?"

2.

"My include directory points to a folder containing a folder called SDL2, so I use #include <SDL2/SDL.h>"

But in your code there is no SDL2: "#include<SDL.h>".

3.

"Also I link to:  -lSDL2main -lSDL2"

Also you didn't say what was the actual error message. It couldn't find the library file, or the include file? So the linking failed or the compilation?

My guess is that your problem is that you use a Macintosh framework, which is a special beast. You have to use the -framework option to link it, and possible the -F option to set the path where the frameworks are.

Share on other sites

a) ensure that the c++ command is indeed clang++; running c++ --version should report "Apple LLVM [snip]...". (c++ is intended to be a symbolic link to clang++ methinks)

b) it is likely that you need to also pass -std=c++11 -stdlib=libc++ to the compiler

An example from one of my Makefiles:

/usr/bin/clang++ -std=c++11 -stdlib=libc++ -I${HOME}/Library/Frameworks/SDL2.framework/Headers -F${HOME}/Library/Frameworks -framework SDL2 pixels.cpp -o pixels

Cheers!

Edited by JeffCarp

Share on other sites

ensure that the c++ command is indeed clang++

If they're intentionally using G++, wouldn't using a different compiler be counter-productive? Perhaps I misunderstood.

Share on other sites

ensure that the c++ command is indeed clang++

If they're intentionally using G++, wouldn't using a different compiler be counter-productive? Perhaps I misunderstood.

Apple uses the LLVM build tool chain (clang is a front-end compiler for it). (If you execute gcc --version, you ought to see "Apple LLVM" somewhere in the output).

On second thought (after reading my post a second time), there's no need for you to pass -std=c++11 -stdlib=libc++ if you are not interested in compiling with the C++11 feature set.

You actually have to jump through a few hoops to get GNU's gcc installed; Homebrew or perhaps another package management system is your best bet for if you were insistent in wanting to use GNU's build tool chain.

Edited by JeffCarp

Share on other sites

I found the correct answer for this. If your a Mac user this will be very helpfull. So im going to sum up what I found in case anybody else stumbles on this. Many of the suggestion above are correct on a nix system but were not helpful for me. Ill show why.

The gnu compiler for mac is the same, but the gnu linker has some mac specific options the dev's put in so that we may use the traditional mac framework conventions. The -Lpath option is a unix convention and thats what wasn't working for me. To compile my helloworld example I had to change the include statement to...

#include<SDL2/SDL.h>

Then the correct compile command is  c++ -o example helloworld.cpp -framework SDL2  Everything here is tandard stuff except -framework SDL2.

-framework SDL2 tells the linker that Im using a framework that is in the default /Library/Frameworks location and then searches the default path /Library/Frameworks.SDL2.framework/Headers for the header file in the include statement. This allows the source code to be identical on whatever system but the linker then knows what to do different because im using a mac.

c++ or g++ are the same thing. On my system they both are symlinks to the current gnu cplus compiler. So either works fine.

Multiple frameworks can be passed by just seperating them by commas. Like -framework SDL2,SDL2_mixer for example

The -Ipath option can be used on a mac to specify the location of source code that is not in the default location. The -Ipath compile option works just as it does on nix systems, follows sym links etc just as you would expect.

JeffCarp noted that apple uses the LLVM build chain but my test compile did not use it (I think). Im fairly sertain that LLVM wil assit in the creation of the Mac specific application.app program distribution method. I didn't look into this because Im just playing around so far and as long as i can execute my program with ./example im fine with that.

zaphod2 noted that I did not link to SDL2main. The binary distribution for windows does contain an SDL2main.dll file (I believe), but my mac distro does not. There is only one binary with the development package im using and that is called SDL2. However I did find there is an sdl_main.h header file and this header is loaded via an include statement from SDL.h so I think im fine here.

Cheers.

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 34
• 16
• 11
• 12
• Forum Statistics

• Total Topics
634123
• Total Posts
3015650
×