简体   繁体   English

没有Xcode的macosx上的C ++ SDL

[英]C++ SDL on macosx without Xcode

System: black Macbook running Mac os X 10.5.5 (Leopard) 系统:黑色Macbook运行Mac OS X 10.5.5(Leopard)

I want to compile an SDL hello-world application using only g++. 我想只使用g ++编译一个SDL hello-world应用程序。 Xcode is good for macintosh but I want cross-platform compatibility, so I won't use any of the coaca framework (no menus, no buttons, etc). Xcode适用于macintosh,但我想要跨平台兼容性,所以我不会使用任何coaca框架(没有菜单,没有按钮等)。 Also, porting Xcode projects to other os's is not something that sounds fun. 此外,将Xcode项目移植到其他操作系统并不是很有趣。 I downloaded and installed SDL into /Library/Frameworks. 我下载并将SDL安装到/ Library / Frameworks中。

The big question is: what goes in the makefile (assuming just a helloWorld.cpp file in the source). 最大的问题是:makefile中的内容(假设源代码中只有一个helloWorld.cpp文件)。 I would like to avoid modifying the Helloworld file found here if possible. 如果可能的话,我想避免修改此处找到的Helloworld文件。

Took me a little while to figure this out myself, only I was already using SDL and was transitioning from C to C++ on Lion. 我花了一点时间自己解决这个问题,只有我已经在使用SDL并且正在从Lion转换为C ++。 Its not just the makefile that is the issue here, its probably your source file as well... 它不仅仅是makefile这里的问题,它可能也是你的源文件......

Download SDL- version .tar.gz 下载SDL- 版本 .tar.gz

Extract and run at prompt 提取并在提示符下运行

./configure --prefix=/home/user/SDL && make && make install

Assuming everything actually built, you now can use the sdl-config to build your source by executing: 假设实际构建了所有内容,您现在可以通过执行以下命令来使用sdl-config来构建源代码:

g++ Main.cpp -o Main `/home/user/SDL/sdl-config --cflags --libs` \
    -framework OpenGL -framework Cocoa

which is the same as 这是一样的

g++ Main.cpp -o Main -I/home/user/SDL/include \
    -L/home/user/SDL/lib -lSDLmain -lSDL -framework OpenGL -framework Cocoa

Now the key here is that you are using C++... for SDL macros to correctly replace your main, you need to prevent the C++ compiler from mangling you main function call. 现在关键是你正在使用C ++ ...对于SDL宏来正确地替换你的main,你需要阻止C ++编译器破坏你的主函数调用。 To do this declare your main like the following: 要做到这一点,请声明您的主要内容如下:

extern "C" int main(int argc, char ** argv)
{
}

If you don't include the extern "C" stuff, C++ will change the name of your main function and SDL won't be able to automatically find it. 如果你不包含extern“C”内容,C ++将更改主函数的名称,SDL将无法自动找到它。 If you don't use the int main(int argc, char ** argv) function signature, C++ will complain about type mismatches... so you've got to do it verbatim. 如果你不使用int main(int argc,char ** argv)函数签名,C ++会抱怨类型不匹配......所以你必须逐字逐句。 (If using GCC, you exclude the extern "C" portion) (如果使用GCC,则排除extern“C”部分)

Chenz Chenz

Try this: 尝试这个:

all: helloWorld

helloWorld: helloWorld.o
    g++ -o helloWorld helloWorld.o `sdl-config --libs`

helloWorld.o: helloWorld.cpp
    g++ -c `sdl-config --cflags` helloWorld.cpp

sdl-config is a tool which should have come with your SDL install that outputs appropriate compiler and linker flags for when compiling with SDL. sdl-config是一个应该随SDL安装一起提供的工具,它在用SDL编译时输出适当的编译器和链接器标志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM