-1

I wrote the CMakeLists.txt file with the following file structure on Linux

-main
--include
--bin
--lib
----SRC
------CMakeLists.txt
------dir1
--------CMakeLists.txt
------dir2
--------CMakeLists.txt

dir1/CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
set(HOME "/workspace/main")
set(INCLUDE "${HOME}/include")
set(LIB "${HOME}/lib")
set(LIBRARY_OUTPUT_PATH "${HOME}/lib")
set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_CXX_COMPILER "g++")
set(CMAKE_C_FLAGS_DEBUG "xxx")
set(CMAKE_CXX_FLAGS_DEBUG "xxx")
include_directories(${INCLUDE} ${INCLUDE}/AR ${INCLUDE}/linux-x86_64)
add_library(video STATIC video.c video2.c)

dir2 directory CMakeLists.txt similar content

SRC/CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
add_subdirectory(dir1)
add_subdirectory(dir2)

My question is: How to write CMakeLists.txt in order to facilitate the transplant on Linux programs to Windows?

H S T
  • 27
  • 5

2 Answers2

1

Don't use absolute paths, obviously. Most CMake commands work relative to root CMakeLists.txt, so there is no need in defining HOME.

arrowd
  • 33,231
  • 8
  • 79
  • 110
  • How can we set the relative path, if `set (XX, ".. /../include")`,`${XX}/YY` would be an illegal path.The implementation of `cmake` will not find the corresponding header file. – H S T Feb 17 '17 at 06:41
  • @HST Did you tried that? There is no reason why this shouldn't work. – arrowd Feb 17 '17 at 07:16
1

Variable CMAKE_SOURCE_DIR refers to the top level project directory. Use this variable instead of HOME for refer to directories in your project.

Alternatively, for being able to build subprojects as standalone projects, you may use PROJECT_SOURCE_DIR for refer to directory of the current project. Note, that you need to have project() in CMakeLists.txt for such subprojects (otherwise most of CMake commands will not work).

lib/SRC/dir1/CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)
project(dir1)
set(HOME "${PROJECT_SOURCE_DIR}/../../..")
...

Also, there is variable CMAKE_CURRENT_SOURCE_DIR which always refers to current source directory (which CMakeLists.txt is executed).

lib/SRC/dir1/CMakeLists.txt:

...
set(HOME "${CMAKE_CURRENT_SOURCE_DIR}/../../..")
...
Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
  • I added a statement that displays the `CMAKE_SOURCE_DIR` value in `CMakeLists.txt` under `dir1`. When I `cmake` in the `dir1` directory, the value of the variable `/workspace/main/lib/SRC/dir1`, in the SRC directory `cmake`, the variable value is `/workspace/main/lib/SRC` – H S T Feb 17 '17 at 07:43
  • 1
    Your question doesn't stay, that you want to call `cmake` from `dir1`. As opposite, it has `SRC/CMakeLists.txt` which descends into subdirectories. Also, `dir1/CMakeLists.txt` doesn't contain `project()` call, so currently it cannot be builded separately. So, do you want subprojects `dir1` and `dir2` to be built separately? If so, add notion about this into the question post. – Tsyvarev Feb 17 '17 at 07:51
  • yes.I would like to do every library under the `SRC` directory can be built separately.Do you have any good way? – H S T Feb 17 '17 at 08:46
  • I have updated the answer post for that case. – Tsyvarev Feb 17 '17 at 13:10