4

I want to create a library with Visual Studio 2010 / VC10 and CMake.

The tree of Windows is not the same than the CMake project tree. The Problem is that CMake doesn't creates the foolib with header and source files in Visual Studio.

I can't change the tree of the library because it's an old grown code with a lot of libraries that share several include files.

root
|-'includes
|  '-foo.h
|-'src  
|  '-libprojects
|  | '-foolib
|  | | '-bin
|  | | '-project
|  | | | '-mak100
|  | | | '-CMakeLists01.txt
|  | | '-src
|  | | | '-CMakeLists02.txt
|  | | | '-foo.cxx

The CMakeLists.txt only has a number to explain.

CMakeLists01.txt

cmake_minimum_required (VERSION 2.8)
cmake_policy (SET CMP0015 NEW)
project (foolib)

set (CMAKE_BUILD_TYPE Debug)

include_directories ("${PROJECT_SOURCE_DIR}/../../../../include")

# This dosen't works and CMake can't find the CMakeLists02.txt ??? 
add_subdirectory("${PROJECT_SOURCE_DIR}/../src")

CMakeLists02.txt

# CMakeLists02.txt
set (QueryHeader
    "./../../../../include/foo.h")

set (QuerySources
    "foo.cxx")

Question: How can I include CMakeLists02.txt into CMakeLists01.txt with add_subdirectory()

This is a batch file if somebody test it

#doCMake.cmd
@echo off
call "c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tool\vsvars32.bat"
mkdir mak100
cd mak100
cmake -G "Visual Studio 10" ..
cd ..
pause
Cœur
  • 37,241
  • 25
  • 195
  • 267
post4dirk
  • 313
  • 4
  • 16
  • I don't get your problem and thus I don't understand your question, if you posted a question. Would you mind to explain a little bit more what you expect and what you actually get? – usr1234567 Oct 02 '15 at 08:30
  • 1
    Possible duplicate of [CMAKE add sub-directory which is not sub-directory on real directory](http://stackoverflow.com/questions/7980784/cmake-add-sub-directory-which-is-not-sub-directory-on-real-directory) – LPs Oct 02 '15 at 09:02
  • @ago. your going right! – post4dirk Oct 02 '15 at 09:27
  • @usr1234567: The questition is what should I write into the "add_subdirectory" line at CmarkeLists01.txt to include CMakeList02. – post4dirk Oct 02 '15 at 09:42
  • See also [CMake with include and source paths - basic setup](http://stackoverflow.com/questions/8304190/cmake-with-include-and-source-paths-basic-setup). Why not put the main/libraries `CMakeLists01.txt` into the `foolib` folder? Then I think you even won't need `CMakeLists02.txt`. Doing something line `add_library(foo src/foo.cxx)` is totally ok. – Florian Oct 02 '15 at 09:46

1 Answers1

2

I just have given your example a try and the solution is given in the error message

CMake Error at CMakeLists.txt:10 (add_subdirectory):
  add_subdirectory not given a binary directory but the given source
  directory ".../src/libprojects/foolib/src"
  is not a subdirectory of
  ".../src/libprojects/foolib/project".  When
  specifying an out-of-tree source a binary directory must be explicitly
  specified.

So as @LPs has pointed out, see CMAKE add sub-directory which is not sub-directory on real directory. Just change your add_subdirectory() call to something like:

add_subdirectory("../src" "src")

And you won't have to prefix the 1st parameter with ${PROJECT_SOURCE_DIR} and the 2nd with ${CMAKE_CURRENT_BINARY_DIR} (both are the defaults, see add_subdirectory()).

My recommendation in your cause would be to put the main/libraries CMakeLists01.txt into the foolib folder. Then you even won't need CMakeLists02.txt.

src/libprojects/foolib/CMakeLists.txt:

cmake_minimum_required (VERSION 2.8)

project (foolib CXX)

include_directories("../../../include")

add_library(foo "src/foo.cxx")

Especially in cases where source and header files are in separate (sub-)folders, doing something like add_library(foo src/foo.cxx) is totally ok/often used.

Community
  • 1
  • 1
Florian
  • 39,996
  • 9
  • 133
  • 149
  • Thanks a lot. Your solution to use only one CMakeLists.txt is good. In my case I have to hide the CMakeLists.txt file in the project folder, but this will be no problem. In a case of 20 or more C Files I'll take the 2 CMakeLists.txt solution because of the clarity. – post4dirk Oct 02 '15 at 12:18
  • @user3355421 You are welcome. Regarding the clarity of source files you may be also interested in [Keeping file hierarchy across subdirectories in CMake](http://stackoverflow.com/questions/31538466/keeping-file-hierarchy-across-subdirectories-in-cmake) – Florian Oct 02 '15 at 12:31