体系结构x86_64的未定义符号,链接器命令失败
Undefined symbols for architecture x86_64, linker command failed
我刚开始学习 c++ 和 makefiles。现在我被困住了。像这样的问题似乎有几十个,但我无法弄清楚哪个答案适用于我的情况。 这个答案大概是显而易见的。一些关于在哪里寻找的线索和我所做的事情是错误的原因将不胜感激!
这是我的错误:
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
这是我的代码:
主.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;
}
应用程式
#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__) */
应用.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;
}
最后是我的制作文件
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) $< -o $@
应该是
$(CXX) $(LDFLAGS) $^ -o $@
由于$<
伪变量仅扩展到第一个依赖项,即main.o
.这与链接器错误匹配。
实际上,仅进行此修改就可以使错误在我的机器上消失。
相关文章:
- Netbeans 10:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- 链接器命令失败,macOS 上的退出代码为 1(使用 -v 查看调用)
- CMake 错误:链接器命令失败,退出代码为 1 和 cpp.o 文件
- clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用) - 体系结构的未定义符号 x86_64:
- 如何修复 clang: 错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- 如何从命令行链接C++库
- Xcode:链接器命令失败,退出代码为 1(使用 -v 查看调用)[C++]
- Android NDK.Build命令失败.未定义的引用.clang++:错误:链接器命令失败,退出代码为1
- 链接从命令行转换为Make的库
- 错误:链接器命令失败,退出代码为 1(使用 -v 查看调用):在 Macbook 上
- C++ XCODE ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -
- Android Studio 链接器命令在包含 Boost 库时失败
- Cmake 3.8.0在makefiles中生成错误的链接命令
- 为什么我在我的C 代码中获得链接命令失败错误
- Xcode C :链接命令失败,出口代码1未找到-lboost_regex
- VS 2008:链接命令包括我没有要求且不存在的库
- CMake:运行链接命令时出错:%1不是有效的Win32应用程序
- 如何在VS 2012中查看c++链接命令
- CMake(Android工具链)-运行链接命令时出错:没有这样的文件或目录
- 如何使用CMake在链接命令行结束时添加标志?