如何使用 -v 调用查看有关 cmake 链接器错误(未定义符号)的详细信息?

How to use -v invocation to see details on cmake linker error (undefined symbols)?

具体如何使用 -v 调用来查看有关 cmake 链接器错误的详细信息?我发现了两个关于使用此选项的现有问题,但一个是针对 Xcode 构建的,另一个是针对 NDK 构建的。他们在这里:

How to use cmake -v invocation to help find linker error

我在 OSX 莫哈韦沙漠。我使用的是标准 cmakelists.txt 方法,错误是这样的:

Undefined symbols for architecture x86_64:
   "Image::createImage(int, int, int)", referenced from:
       _main in tutorial.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

SO 问题中的评论显示了我正在尝试做的事情:

...you could use -v to see the linker invocation to see what's going wrong. It would show you this link line:

"/usr/bin/ld" -demangle -dynamic -arch x86_64 
    -macosx_version_min 10.6.8 -o a.out -lcrt1.10.6.o
    /var/folders/zl/zlZcj24WHvenScwjPFFFQE+++TI/-Tmp-/cc-hdOL8Z.o
    -lSystem /Developer/usr/bin/../lib/clang/3.0/lib/darwin/libclang_rt.osx.a

(顺便说一句,我是 cmake 菜鸟)我已经在我的 cmakelists.txt 中尝试了以下所有方法,但它们没有用:

add_link_options(-v)
add_link_options("-v")
target_link_options(myexec PUBLIC -v)
target_link_options(myexec PUBLIC "-v")
target_link_options(myexec PUBLIC "LINKER:-v")

最简单的是普通的 VERBOSE=1 make 而不是 make(如果您使用 make)。

输出是这样的:

VERBOSE=1 make      
[100%] Linking C executable example
/usr/bin/cmake -E cmake_link_script CMakeFiles/example.dir/link.txt --verbose=1
/usr/bin/cc   -rdynamic CMakeFiles/example.dir/main.c.o  -o example 
make[2]: Leaving directory '../c-examples/cmake/build'
[100%] Built target example
make[1]: Leaving directory '../c-examples/cmake/build'
/usr/bin/cmake -E cmake_progress_start ../c-examples/cmake/build/CMakeFiles 0

你看这里调用了cc。在您的情况下,您使用的是 clang(也可能是 gcc)。

让我们假设你已经做了类似 sudo update-alternatives --config cc 的事情 - 或者你在 OS 上组织符号链接的任何其他方式 - 将 clang 作为你的默认编译器。现在,如果您只键入 cc -v,您会看到它会显示 version 信息。您宁愿要求链接器冗长。这是通过 -Wl-Xlinker.

完成的

在你的情况下,像这样的 CMakeLists.txt 将提供充足的信息:

# Note that this is an older cmake version, use target_link_options if available
cmake_minimum_required (VERSION 2.6)

# Project name (in this case a simple C project where the math dep is "forgotten")
project(mathdep_example)

# Not your situation, but in case you have a different linker
# set(CMAKE_EXE_LINKER_FLAGS "-Wl,--verbose")

# Clang passes flags through to the linker (likely ld) by
# set(CMAKE_EXE_LINKER_FLAGS "-Xlinker -v")

# Clang passing flags to the linker (likely ld) AND using -v itself to show how it calls the linker
set(CMAKE_EXE_LINKER_FLAGS "-Xlinker -v -v")

# The final executable
add_executable(example main.c)

# In case you want also verbose compilation steps
target_compile_options(example PRIVATE -v)

观察到 -v 在链接器标志中出现了两次。第一个以 -Xlinker 开头的将被传递给链接器(可能是 ld)。第二个是 clang 本身在链接器步骤的选项。请注意,clang 仍然告诉您添加 -v,即使您确实这样做了。这可能被认为是一个错误...

对于您的情况,我会检查您使用的是哪种 .o.a 文件。听起来它们不适合您的架构。使用 file:

file object_file.o
file archive_file.a