编译时未检测到主要功能

时间:2019-05-10 18:47:25

标签: c++ main

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

但是当我出于某种原因运行代码时,出现此错误:

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]

先谢谢您

1 个答案:

答案 0 :(得分:4)

在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安装到*.pc的{​​{1}}文件相对应。