本文介绍了体系结构x86_64的未定义符号,链接器命令失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我刚刚开始学习c ++和makefile.现在我被卡住了.似乎有很多这样的问题,但是我不知道哪个答案适用于我的情况.这个答案可能很明显.对于在哪里看的线索以及我在做什么的原因是错误的,将不胜感激!

I just started learning c++ and makefiles. Now I'm stuck.There seem to be dozens of questions like this, but I can't figure out which answer applies to my situation. This answer is probably obvious. Some clues for where to look and reasons for what I am doing is wrong would be greatly appreciated!

这是我的错误:

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

这是我的代码:

main.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;
}

最后是我的 makefile

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)

推荐答案

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

You are not linking all the object files.

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

应该是

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

因为 $< 伪变量扩展为仅第一个依赖项,即 main.o .符合链接器错误.

since the $< pseudo-variable expands to only the first dependency, which is main.o. That matches with the linker error.

实际上,仅进行此修改即可使错误在我的计算机上消失.

这篇关于体系结构x86_64的未定义符号,链接器命令失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 08:29