【问题标题】:ld: symbol(s) not found for architecture x86_64 using GNU Make and clangld:使用 GNU Make 和 clang 找不到架构 x86_64 的符号
【发布时间】:2012-12-16 10:10:27
【问题描述】:

我收到ld: symbol(s) not found for architecture x86_64 错误。我确实通过以前的线程进行了彻底的搜索,但没有找到解决方案。

作为测试,我正在编译一个程序 test.cpp:

#include <iostream>

using namespace std;

int main(){
 return 0;
}

使用 Makefile:

CXX=g++
CXXFLAGS=-g -Wall -W -Wconversion -Wshadow -Wcast-qual -Wwrite-strings $(shell root-config --cflags --glibs)
CPPFLAGS+=-MMD -MP
LDFLAGS=-g $(shell root-config --ldflags)
LDLIBS=$(shell root-config --libs)

test.o: test.cpp

test: test.o
-include test.d

在我的 linux 计算机上运行 make test 会产生以下正确输出:

CAM ~/Wenu $ make test
g++ -g -Wall -W -Wconversion -Wshadow -Wcast-qual -Wwrite-strings -pthread -m64 -I/cvmfs/lhcb.cern.ch/lib/RootConfig/pro/x86_64-slc5-gcc46-opt/root/include -L/cvmfs/lhcb.cern.ch/lib/RootConfig/pro/x86_64-slc5-gcc46-opt/root/lib -lGui -lCore -lCint -lRIO -lNet -lHist -lGraf -lGraf3d -lGpad -lTree -lRint -lPostscript -lMatrix -lPhysics -lMathCore -lThread -pthread -lm -ldl -rdynamic -MMD -MP  -c -o test.o test.cpp
cc -g -m64  test.o  -L/cvmfs/lhcb.cern.ch/lib/RootConfig/pro/x86_64-slc5-gcc46-opt/root/lib -lCore -lCint -lRIO -lNet -lHist -lGraf -lGraf3d -lGpad -lTree -lRint -lPostscript -lMatrix -lPhysics -lMathCore -lThread -pthread -lm -ldl -rdynamic -o test
CAM ~/Wenu $

在我的 Mac 上编译它在链接阶段失败并出现 ld: symbol(s) not found for architecture x86_64 错误:

MAC ~/Transmit/Wenu $ make test
g++ -g -Wall -W -Wconversion -Wshadow -Wcast-qual -Wwrite-strings -pthread -m64 -I/opt/local/root-v5-34-00/include -L/opt/local/root-v5-34-00/lib -lGui -lCore -lCint -lRIO -lNet -lHist -lGraf -lGraf3d -lGpad -lTree -lRint -lPostscript -lMatrix -lPhysics -lMathCore -lThread -lpthread -Wl,-rpath,/opt/local/root-v5-34-00/lib -lm -ldl -MMD -MP  -c -o test.o test.cpp
i686-apple-darwin11-llvm-g++-4.2: -lGui: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -lCore: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -lCint: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -lRIO: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -lNet: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -lHist: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -lGraf: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -lGraf3d: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -lGpad: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -lTree: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -lRint: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -lPostscript: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -lMatrix: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -lPhysics: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -lMathCore: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -lThread: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -lpthread: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -rpath: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: /opt/local/root-v5-34-00/lib: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -lm: linker input file unused because linking not done
i686-apple-darwin11-llvm-g++-4.2: -ldl: linker input file unused because linking not done
cc -g -m64  test.o  -L/opt/local/root-v5-34-00/lib -lCore -lCint -lRIO -lNet -lHist -lGraf -lGraf3d -lGpad -lTree -lRint -lPostscript -lMatrix -lPhysics -lMathCore -lThread -lpthread -Wl,-rpath,/opt/local/root-v5-34-00/lib -lm -ldl -o test
Undefined symbols for architecture x86_64:
  "std::ios_base::Init::Init()", referenced from:
      __static_initialization_and_destruction_0(int, int) in test.o
  "std::ios_base::Init::~Init()", referenced from:
      ___tcf_0 in test.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: *** [test] Error 1

任何帮助将不胜感激。

如果其中任何一个相关,我有一台装有 OS X 10.7.5 的 64 位 Mac,并且我有一个带有 XCode 命令行工具的编译器:

MAC ~/Transmit/Wenu $ clang --version
Apple clang version 4.1 (tags/Apple/clang-421.11.66) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin11.4.2
Thread model: posix

ROOT 是我在程序中使用的 C++ 库的集合。

【问题讨论】:

  • 尝试使用c++ 而不是cc
  • 通常情况下,如果文件名为“name.c”而不是“name.cpp”,它将使用 cc 而不是 c++ 编译。但是,当然,如果您真的想使用 C++ 功能,则需要使用 C++ 库。我很确定您在安装中缺少 C++ 库 - 这很可能是一组要安装的单独文件,我不知道您需要在 Mac 上做什么来安装它(我确定它是很简单...)
  • @MatsPetersson 但如果我手动输入命令 make 生成并用 c++ 替换 cc ,则代码会编译。我的文件也有一个 cpp 扩展名,所以我不明白为什么要使用 cc。如何在 Makefile 中将其设置为 c++?
  • @JamesMcLaughlin 是的,消除了错误。如何在我的 Makefile 中将其设置为使用 c++ 而不是 cc?
  • 啊,我刚刚发现了问题所在。您也应该将 g++ 用于 LINKING 阶段。在你的makefile中设置LD=g++,它应该会自行解决。

标签: c++ compiler-construction linker 64-bit clang


【解决方案1】:

在我的 linux 计算机上运行 make test 会产生以下正确输出

这只是偶然的:您正在将C++ 程序与cc 链接。如果您使用更多 C++,它将停止在 Linux 上工作,例如尝试将cout &lt;&lt; "Hello" &lt;&lt; endl; 放入您的main

我的文件还有一个 cpp 扩展名,所以我不明白为什么要使用 cc。

因为从test.o 生成test 的默认make 规则使用$CC,而不是$CXX

您可以通过以下几种方式之一解决此问题:

  • 强制$CC 也成为g++,或者
  • test.o 生成test 时将Makefile 更改为使用非默认规则,或者
  • 使test 依赖于test.cpp(如果您这样做,make 将使用正确的默认规则)。

【讨论】:

    猜你喜欢
    • 2019-02-21
    • 1970-01-01
    • 1970-01-01
    • 2011-10-16
    • 1970-01-01
    • 2013-11-18
    • 2020-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多