【问题标题】:main function not being detected when compiled编译时未检测到主函数
【发布时间】:2019-09-28 16:46:17
【问题描述】:

我正在尝试运行一个打开窗口的程序。目的是让程序启动打开一个窗口是所有程序的启动对吗?

但是当我出于某种原因运行我的代码时,我得到了这个错误:

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]

提前谢谢你

【问题讨论】:

  • 你是怎么编译的?您可以发布您正在使用的 clang 命令吗?
  • clang 命令是什么意思?
  • 很明显,您提供的源确实具有main() 函数。要么它不是与您提出的错误相关的来源,要么问题与您尝试编译程序的方式有关。那么您是如何尝试编译程序的呢?
  • "打开一个窗口是所有程序的开始,对吧?" - 不。有很多非常有用的控制台应用程序从不打开任何窗口/GUI。以grep为例;如果它打开了一扇窗户,我会很不高兴。

标签: c++ main


【解决方案1】:

在 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 软件包的名称对应于 Homebrew 安装到 /usr/local/lib/pkgconfig 中的 *.pc 文件。

【讨论】:

  • 其他来源表明 SDL 标头本身提供宏定义。无论如何,缺少-lSDL2main 似乎确实是核心问题。
  • @JohnBollinger 如果您使用 pkg-config,将使用正确的标志。
  • 无论我如何为 sdl 图像和 sdl ttf 做到这一点,这都行得通?
  • @MikahShattuck 您可以同时传递两个参数:pkg-config --cflags SDL2_ttf SDL2_image--libs 也一样)。
  • @MikahShattuck 我忘记了 Homebrew 是否默认安装 pkg-config。如果没有,请自行安装:brew install pkg-config
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-13
  • 1970-01-01
  • 2015-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多