3

I am trying to use a header only library (thread-pool) as a sub project. So in root CMakeLists.txt I have

ADD_SUBDIRECTORY(thread_pool)

inside thread-pool/CMakeLists.txt

CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
PROJECT(thread-pool)

SET(THREAD_POOL_SOURCES)

ADD_LIBRARY(thread-pool STATIC ${THREAD_POOL_SOURCES})

As this is a header only library with no source, it gives error.

CMake Error: CMake can not determine linker language for target: thread-pool

One solution is to use ADD_LIBRARY(thread-pool INTERFACE) but that only works with CMake 3.0 and I've 2.8 installed. I am not asking for a solution on how to upgrade CMake to 3.0 but is there any alternative way that I can use with CMake 2.6 or 2.8 ?

One way that comes in my mind is to have a fake cpp file with some dummy function and put that in sources, But that will be a bad solution.

Neel Basu
  • 12,638
  • 12
  • 82
  • 146
  • 1
    Don't count CMake version like 2.6, 2.8, 3.0. It is more like 2.6, 2.8.0, 2.8.1, ..., 2.8.12, 3.0. They changed the scheme of version numbers. Between 2.8.x and 2.8.x+1 CMake gained several features for every x in 0, 11. – usr1234567 Mar 16 '15 at 13:46

2 Answers2

1

To your original issue, try using

set_target_properties(thread-pool PROPERTIES LINKER_LANGUAGE CXX)
StAlphonzo
  • 736
  • 5
  • 14
  • It doesn't need source for the properties to work. I have tested this approach on versions as low as 2.8.10. I suppose it's possible not to work on 2.6, but considering that's going on 6 years old YMMV. – StAlphonzo Mar 17 '15 at 10:47
0

Assuming you have unpacked the library in the thread-pool subdirectory in the root of your source directory, and your project structure looks something like this:

(root)
 |-- thread-pool
 |   +-- boost
 |       +-- threadpool.hpp
 |-- subproject_A
 |   +-- test.cpp
 +-- CMakeLists.txt

you only need to do in the root CMakeLists.txt:

include_directories(thread-pool)
...
add_subdirectory(subproject_A)    

to gain access to the library. Then in test.cpp, all you need to do is #include "boost/threadpool.hpp" with no relative path.

add_library is only required for libraries that have a compiled component, so don't use it.

atsui
  • 1,008
  • 10
  • 17
  • But that requires me to add relative paths for all other sub projects that uses thread pool. I cannot just say include threadpool to resolve path automatically – Neel Basu Mar 15 '15 at 09:02
  • No, you don't need to add relative paths because cmake searches the subdirectory you added for include files. See my updated answer. Is this how you intend to use your project? – atsui Mar 15 '15 at 15:02