Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting CMake to give an error/warning about unreferenced symbols

Tags:

cmake

I'm wondering how I would go about making CMake produce an error, or at least a warning, when the linker cannot find symbols that are referenced in a source file?

For example, let's say I have foo.c:

#include "bar.h" //bar.h provides bar()

void foo(void)
{
  bar()
  return;
}

In the case that I am building a static library, if i am not smart about how i have used my add_library() directive, the default behavior seems to be to not even give a warning that bar is an unreferenced symbols in foo's object archive file (.a)

like image 614
bkinman Avatar asked Dec 30 '25 17:12

bkinman


1 Answers

The CMAKE_SHARED_LINKER_FLAGS compiler flags for building shared libraries should get the compiler to do what you want.

set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--no-undefined") On Unix systems, this will make the linker report any unresolved symbols from object files (which is quite typical when you compile many targets in CMake projects, but do not bother with linking target dependencies in proper order).

Source: http://www.cmake.org/Wiki/CMake_Useful_Variables

like image 114
Beginner Avatar answered Jan 02 '26 05:01

Beginner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!