c++ - 编译时未检测到主函数
问题描述
我正在尝试运行一个打开窗口的程序。目的是让程序启动打开一个窗口是所有程序的启动对吗?
但是当我出于某种原因运行我的代码时,我得到了这个错误:
Undefined symbols for architecture x86_64: "_main", referenced from: implicit entry/start for main executable ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
但是,在我的代码中,我确实有这个main()
功能,为什么我会收到这个错误?
这是我的代码:
#include <SDL2/SDL.h>
#include <SDL2_image/SDL_image.h>
#include <SDL2_ttf/SDL_ttf.h>
#include <stdio.h>
int main(){
if(SDL_Init( SDL_INIT_EVERYTHING ) < 0){
std::cout << "error 1\n";
std::cout << SDL_GetError();
std::cout << "\n";
return -1;
}
if(TTF_Init() < 0){
std::cout << "error 2\n";
std::cout << TTF_GetError();
std::cout << "\n";
return -1;
}
SDL_Window* window = SDL_CreateWindow("test", 0, 0, 500, 500, 0);
if(!window){
std::cout << "error 3\n";
std::cout << SDL_GetError();
std::cout << "\n";
return -1;
}
int windowid = SDL_GetWindowID(window);
SDL_Renderer* Renderer = SDL_CreateRenderer(window, -1, 0);
running = true;
SDL_Event event;
while(running){
while(SDL_PollEvent(&event)){
if(event.type == SDL_WindowEvent){
if(event.window.windowID == windowid){
if(event.window.type == SDL_WindowClose){
Destroywindow(window);
running = false;
}
}
}
}
}
return 0;
}
我的 make 文件如下所示:
#!/bin/bash
brew update
brew install sdl2
g++ -o /Users/mikahshattuck/noneproject/none2019-05-0909-22-
14:2:/none.app/Contents/MacOS/mainrun.cpp -I /Library/Frameworks -l
SDL2
exit 0
这是完整的:
Already up-to-date.
Warning: sdl2 2.0.9_1 is already installed and up-to-date
To reinstall 2.0.9_1, run `brew reinstall sdl2`
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see
invocation)
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
[Process completed]
先感谢您
解决方案
在 macOS 或 Windows 上使用 SDL 时,您需要添加-Dmain=SDL_main
到编译标志和-lSDL2main
链接标志。由于您使用的是 Homebrew,因此您可以更轻松地使用 pkg-config 来获取正确的标志。将此编译器命令用作模板并根据您的需要进行调整:
g++ $(pkg-config --cflags sdl2) -I /Library/Frameworks source.cpp -o output_executable $(pkg-config --libs sdl2)
但是,您似乎也在使用 SDL_ttf,而不仅仅是普通的 SDL。在这种情况下,您可能应该使用SDL2_ttf
而不是sdl2
作为 pkg-config 的包参数:
g++ $(pkg-config --cflags SDL2_ttf) -I /Library/Frameworks source.cpp -o output_executable $(pkg-config --libs SDL2_ttf)
SDL2_ttf 包依赖于 sdl2 包,因此使用 SDL2_ttf 也会发出 sdl2 所需的标志。
pkg-config 软件包的名称对应于*.pc
Homebrew 安装到/usr/local/lib/pkgconfig
.
推荐阅读
- python - 将事件添加到正在运行的 sched.scheduler
- c# - 使用 GCHandle.Alloc() 访问和操作固定内存的正确方法是什么?
- scikit-learn - 将决策树设为弱学习器,但得分 > 80%
- java - firebase.auth.FirebaseUser.linkWithCredential 抛出空指针异常
- javascript - 读取本地 JSON 文件并将其中每个对象的确定键返回到数组中
- java - 检查方法的返回是否为 List
有反射 - java - 为什么我不断收到错误:“错误:(20、23)java:不兼容的类型:java.lang.String 无法转换为 Lab1.Person”
- spring-security - OAuth2FeignRequestInterceptor 的替代方案,依赖于已弃用的类
- lua - lua中的Python itertools产品功能
- gcc - 如何使用 GCC 编译指示优化选项?(例如 max-peeled-insns)