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

Viewed 2391

How specifically do I use the -v invocation to see details on a cmake linker error? I found two existing questions about using this option, but one is for Xcode builds and the other for NDK builds. They are here:

use -v to see invocation?

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

I am on OSX Mojave. I am using a standard cmakelists.txt approach, and the error is this:

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)

A comment in a SO question shows what I am trying to do:

...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

(I'm a cmake rookie btw) I have tried all of the following in my cmakelists.txt but they did not work:

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")
1 Answers

The easiest is a plain VERBOSE=1 make rather than just make (in case you use make).

The output is something like this:

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

You see that's cc is invoked here. In your case, you are using clang (it could have been gcc as well).

Let's assume you have done something like sudo update-alternatives --config cc - or any other way in which you organize symbolic links on your OS - to have clang as your default compiler. Now, if you just type cc -v you will see that it will show version information. You rather want to ask the linker to be verbose. That's done through -Wl or -Xlinker.

In your case, something like this CMakeLists.txt will give ample information:

# 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)

Observe that -v occurs two times in the linker flags. The first preceded by -Xlinker will be passed to the linker (likely ld). The second is an option to clang itself at the linker step. Note, that clang still tells you to add -v even if you actually did so. This might be considered a bug...

In your case I would check what kind of .o and .a files you are using. It sounds like they are not for your architecture. Use file:

file object_file.o
file archive_file.a
Related