Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake library not found

Tags:

cmake

I try to run the makefile that using cmake produced. It generate an error

ld: library not found for -lhello
clang: error: linker command failed with exit code 1 (use -v to see invocation)

the file directory is: enter image description here

the cmakelists.txt is: enter image description here

the main.c file is: enter image description here

the ERROR: enter image description here I think I set the right directory. How to solve this ERROR?

like image 507
user3130007 Avatar asked Feb 01 '26 02:02

user3130007


1 Answers

CMake has a system if you want to link libraries. For many standard libraries we have cmake modules which will allow you to use the find_package command. This will set some variables for include directories and libraries. If there is no such thing for your library you can use find_path for the include files and find_library to search for a library.

Here is what you could do (untested, just out of my head):

add_executable(main main.c)

target_include_directories(
    PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}
    PUBLIC ${CMAKE_SOURCE_DIR}/include/hello
)

find_library (
    HELLO_LIB
    NAMES hello libhello # what to look for
    HINTS "${CMAKE_SOURCE_DIR}/lib" # where to look
    NO_DEFAULT_PATH # do not search system default paths
)

# check if we found the library
message(STATUS "HELLO_LIB: [${HELLO_LIB}]")

if (NOT HELLO_LIB)
    message(SEND_ERROR "Did not find lib hello")
endif

target_link_libraries(main
    ${HELLO_LIB}
)

Use message to debug your cmake files. If you define the library in cmake as well you can link directly against the cmake target.

like image 150
Christian Rapp Avatar answered Feb 03 '26 17:02

Christian Rapp



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!