【问题标题】:Undefined symbols for architecture x86_64, linker command failed架构 x86_64 的未定义符号,链接器命令失败
【发布时间】:2013-12-27 20:16:41
【问题描述】:

我刚开始学习 c++ 和 makefile。现在我被困住了。 这样的问题似乎有几十个,但我不知道哪个答案适用于我的情况。这个答案可能很明显。非常感谢您提供一些关于在哪里寻找的线索以及我做错的原因!

这是我的错误:

Undefined symbols for architecture x86_64:
  "App::init(char const*, int, int, int, int, bool)", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [../out/MYAPP] Error 1

这是我的代码:

ma​​in.cpp

#include "App.h"

// our App object
App* a_app = 0;

int main(int argc, char* argv[])
{
    a_app = new App();

    a_app->init("Hello World", 100, 100, 640, 480, true);

    //...etc more code
    return 0;
}

App.h

#ifndef __App__
#define __App__

#include <SDL2/SDL.h>

class App
{
public:

    App(){}
    ~App(){}

    bool init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen);
    //..etc
private:

};

#endif /* defined(__App__) */

App.cpp

#include "App.h"
#include <iostream>

using namespace std;

bool App::init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen)
{   
    // attempt to initialize SDL
    if(SDL_Init(SDL_INIT_EVERYTHING) == 0)
    {
        cout << "SDL init success \n";

        int flags = 0;
        if(fullscreen)
        {
            flags = SDL_WINDOW_FULLSCREEN;
        }

        // init the window
        m_pWindow = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
            //...etc 

    }
    else
    {
        cout << "SDL init fail \n";
        return false; // SDL init fail
    }

    cout << "SDL init success \n";
    m_bRunning = true; // everything inited successfully, start the main loop

    return true;
}

最后是我的ma​​kefile

CXX = clang++
SDL = -framework SDL2 -framework SDL2_image
INCLUDES = -I ~/Library/Frameworks/SDL2.framework/Headers -I ~/Library/Frameworks/SDL2_image.framework/Headers
CXXFLAGS = -Wall -c -std=c++11 $(INCLUDES)
LDFLAGS = $(SDL) -F ~/Library/Frameworks/
SRC = src
BUILD = build
OUT = ../out
EXE = $(OUT)/MYAPP

OBJECTS = $(BUILD)/main.o $(BUILD)/App.o

all: $(EXE)

$(EXE): $(OBJECTS) | $(OUT)
    $(CXX) $(LDFLAGS) $< -o $@

$(BUILD)/%.o : $(SRC)/%.cpp | $(BUILD)
    $(CXX) $(CXXFLAGS) $< -o $@ 

$(BUILD):
    mkdir -p $(BUILD)

$(OUT):
    mkdir -p $(OUT)

clean:
    rm $(BUILD)/*.o && rm $(EXE)

【问题讨论】:

  • $(CXX) $(LDFLAGS) $&lt; -o $@ 应该是 $(CXX) $(LDFLAGS) $^ -o $@ -- $&lt; 伪变量仅扩展为第一个依赖项,即 main.o。这与链接器错误相匹配。
  • @H2CO3:然后将其作为答案发布。

标签: c++ linker makefile linker-errors


【解决方案1】:

您没有链接所有目标文件。

$(CXX) $(LDFLAGS) $< -o $@

应该是

$(CXX) $(LDFLAGS) $^ -o $@

因为$&lt; 伪变量扩展为只有第一个依赖项,即main.o。这与链接器错误相匹配。

事实上,只做这个修改就可以让我的机器上的错误消失。

【讨论】:

  • 另外,作为一个旁注,如果你没有将它与clang 和基于 OS X LLVM 的工具链链接,你很有可能需要交换链接器标志和目标文件,如$(CXX) $^ $(LDFLAGS) -o $@。
  • 哇,好的!谢谢,这有很大帮助。现在没有更多错误,一切正常。你知道任何适合初学者的好的 makefile 参考吗?我知道有官方文档,但它们有点压倒性。
  • @bottleboot 好吧,我不知道一个具体的全能教程;我记得我花了很长时间才掌握 Makefile,基本上我只是在需要了解特定功能时四处搜索。
猜你喜欢
  • 2013-10-10
  • 2015-12-02
  • 1970-01-01
  • 2015-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多