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

Aly*_*hak 3 c++ macos cmake linker-errors undefined-symbol

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

使用-v查看调用?

如何使用cmake -v调用来帮助查找链接器错误

我在OSX Mojave上。我正在使用标准的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)
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

(我是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")
Run Code Online (Sandbox Code Playgroud)

Ann*_*sum 5

最简单的方法是简单的方法,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
Run Code Online (Sandbox Code Playgroud)

您会看到cc在这里调用了。在您的情况下,您正在使用clang(它本来也可以gcc)。

假设您已经做过类似的事情sudo update-alternatives --config cc-或在操作系统上组织符号链接的任何其他方式- 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)
Run Code Online (Sandbox Code Playgroud)

注意-v链接器标志中发生了两次。第一个前缀-Xlinker将被传递给链接器(可能是ld)。第二个是clang在链接器步骤本身的选项。请注意,即使您确实这样做,它clang仍然会告诉您添加-v。这可能被视为错误...

在你的情况我会检查什么样的.o.a你正在使用的文件。听起来它们不适用于您的体系结构。用途file

file object_file.o
file archive_file.a
Run Code Online (Sandbox Code Playgroud)