本文介绍了C ++ SDL on macosx without Xcode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

系统:黑色Macbook运行Mac OS X 10.5.5(Leopard)

System: black Macbook running Mac os X 10.5.5 (Leopard)

我想使用g ++编译SDL hello-world应用程序。 Xcode适用于Macintosh,但我想要跨平台兼容性,所以我不会使用任何coaca框架(没有菜单,没有按钮等)。另外,将Xcode项目移植到其他操作系统是不是很有趣。我下载并安装SDL到/ Library / Frameworks。

I want to compile an SDL hello-world application using only g++. 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). Also, porting Xcode projects to other os's is not something that sounds fun. I downloaded and installed SDL into /Library/Frameworks.

最大的问题是:makefile中有什么(假设源代码中只有一个helloWorld.cpp文件)。我想避免修改在可能的情况下找到的Helloworld文件。

The big question is: what goes in the makefile (assuming just a helloWorld.cpp file in the source). I would like to avoid modifying the Helloworld file found here if possible.

推荐答案

花了我一点时间自己想出来,只有我已经使用SDL,并在Lion上从C转换到C ++。它不仅仅是这里的问题的makefile,它可能是你的源文件...

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. Its not just the makefile that is the issue here, its probably your source file as well...

下载SDL- 版本 .tar。 gz

Download SDL-version.tar.gz

在提示时提取并运行

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

假设实际构建了一切,现在可以使用sdl-config来构建源:

Assuming everything actually built, you now can use the sdl-config to build your source by executing:

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

现在的关键是使用C ++ ...为SDL宏正确替换你的主,你需要防止C ++编译器从你的main函数调用。要这样做声明你的主要像下面:

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. To do this declare your main like the following:

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

如果不包括 extern C的东西,C ++将更改您的主要功能的名称,SDL将无法自动找到它。如果你不使用 int main(int argc,char ** argv)函数签名,C ++会抱怨类型不匹配...所以你必须逐字处理。 (如果使用GCC,则排除 externC部分)

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. 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. (If using GCC, you exclude the extern "C" portion)

Chenz

这篇关于C ++ SDL on macosx without Xcode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 08:52
查看更多