2

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?

user3130007
  • 663
  • 3
  • 8
  • 17
  • "I think I set the right directory." - You haven't set any directory for **linker**; `include_directories()` sets directories for *compiler*. Have you seen this generic question about library linking: https://stackoverflow.com/questions/8774593/cmake-link-to-external-library/10550334#10550334? – Tsyvarev Dec 07 '17 at 09:11
  • @Tsyvarev I try to add the link_directories(), but still get the same error. – user3130007 Dec 07 '17 at 09:28
  • Yes, sometimes `link_directories` don't work as expected. Try the [second answer](https://stackoverflow.com/a/10550334/3440745): it is more scored than the accepted one. – Tsyvarev Dec 07 '17 at 16:25
  • The second answer worked! well done! – user3130007 Dec 08 '17 at 02:52
  • 2
    Possible duplicate of [CMake link to external library](https://stackoverflow.com/questions/8774593/cmake-link-to-external-library) – Tsyvarev Dec 08 '17 at 07:25
  • @Tsyvarev I find the reason why `link_directories` not work. Because the `link_directories` must set before the `add_executable` – user3130007 Dec 10 '17 at 05:39

2 Answers2

8

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.

Increasingly Idiotic
  • 5,700
  • 5
  • 35
  • 73
Christian Rapp
  • 1,853
  • 24
  • 37
1

When your library isn't in a standard path liking /usr/lib, you should use link_directories() in your CMakeLists.txt to specify a non-standard library path which contains your library. Notice that you MUST put your link_directories() ahead of add_executable() as the following shows:

link_directories(../../lib)
add_executable(newhello main.c)
include_directories(../../include)
target_link_libraries(newhello hello)
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Lh Muraob
  • 11
  • 1
  • Thanks the God. Save my time. I just adjust the line position of link_directories() as you said. Then it compiled succeed. – dong liu Aug 23 '23 at 03:46