Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build systems can't find references when library is built in another project, why?

So I have a library that compiles when compiled by itself using its CMake. I then include this library as a git submodule in another project, and now it fails to find references in the code for some reason.

/usr/bin/ld: libfreedom/libfreedom.so: undefined reference to `drmModeGetResources'
/usr/bin/ld: libfreedom/libfreedom.so: undefined reference to `drmModeGetConnector'
/usr/bin/ld: libfreedom/libfreedom.so: undefined reference to `drmModeFreeConnector'

This is the CMakeLists.txt for the library that builds by itself:

cmake_minimum_required(VERSION 3.10)
project(libfreedom LANGUAGES C)

# Specify the C standard
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# Add library target
add_library(libfreedom SHARED src/libfreedom.c)

target_include_directories(libfreedom PRIVATE /usr/include/libdrm)

# Prevent it from being called liblibfreedom.so
set_target_properties(libfreedom PROPERTIES
    OUTPUT_NAME "freedom"  # This will create libfreedom.so
)

# Install targets and configuration
install(TARGETS libfreedom DESTINATION lib)
install(FILES include/libfreedom.h DESTINATION include)

And this is the CMakeLists.txt for the project. Note that cmake works but then while trying to use make or ninja I get the error messages above.

cmake_minimum_required(VERSION 3.10)

project(Freedom)

# Add the submodule as a dependency
add_subdirectory(libfreedom)

# Create the executable
add_executable(Freedom src/main.c)

# Link the submodule library to the main project
target_link_libraries(Freedom PRIVATE libfreedom)

target_include_directories(Freedom PRIVATE libfreedom/include)
like image 407
CocytusDEDI Avatar asked Jan 21 '26 22:01

CocytusDEDI


1 Answers

Try changing PRIVATE to PUBLIC in target_include_directories(libfreedom PRIVATE /usr/include/libdrm).

I found this: Target command scope in CMake

like image 193
user8300370 Avatar answered Jan 24 '26 17:01

user8300370



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!