1

I started with the following directory structure:

project
       exec
           executable.exe
       lib
           src
           include
       config
           <cmake-generated config file>

I created the library in the lib/src folder by using a CMakefile in the lib/src folder. The exe would compile.

Then, I moved my CMakeFile up to /lib, making sure to change the source file paths to /src/* Now, when I try to compile, all my libraries compile and link fine, but when I try to link the executable, I get /usr/bin/ld: cannot find -lconfig.

Does anyone have any idea why this happens or how to fix it?

Here is some of my code:

./CMakeLists.txt:
     include_directories(config)
     SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
     SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
     ADD_SUBDIRECTORY(libs) #library sources
     ADD_SUBDIRECTORY(exec) #executable sources
             CONFIGURE_FILE(${core_SOURCE_DIR}/config/config.h.in  
                  ${core_SOURCE_DIR}/config/config.h)

 ./libs/CMakeLists.txt:
     file(GLOB src ...)
     file(GLOB header ...)
     add_library(lib ${src} ${header})


 ./exec/CMakeLists:
      add_executable(executable executable.cpp)
      link_directories(${core_SOURCE_DIR}/lib) #not sure if this is required
      target_link_libraries(executable ${lots_of_libs})  

Every library in lots_of_libs can be found as a .a file in the lib directory

7
  • Can you show the content of your CMakeLists.txt? Commented Jul 11, 2013 at 21:44
  • Yeah. I have lots of libraries so I'll try and keep it brief. Commented Jul 11, 2013 at 21:47
  • Can you try to see what happens if you remove the statements SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) and SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib) Commented Jul 11, 2013 at 22:41
  • 1
    Is config somehow in ${lots_of_libs}? Commented Jul 12, 2013 at 3:15
  • 1
    @StevenMorad First thing to check: Add in exec/CMakeLists.txt this: message(STATUS "lots_of_libs ${lots_of_libs}") If config is not there, there must be something tricky in your configure file... Some strange linking flag... I do not know exactly, I have never used a configure file... Commented Jul 12, 2013 at 7:31

1 Answer 1

1

One problem, probably not risolutive, is this:

link_directories(${core_SOURCE_DIR}/lib) #not sure if this is required

should be:

link_directories(${PROJECT_BINARY_DIR}/lib) 

or:

link_directories(${LIBRARY_OUTPUT_PATH}) 

Anyway, normally you wouldn't need to add to your link_directories the path to a library that is built within the project, even if you have specified a different LIBRARY_OUTPUT_PATH

Sign up to request clarification or add additional context in comments.

2 Comments

You never have to specify link_directories for targets you build from the same CMake project. CMake is smart enough to figure this out by itself.
@ComicSansMS You are right, but this doesn't change the fact that line had a wrong path in it. Anyway, I will edit my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.