CMake add_subdirectory ()

Introduction:

I am trying to use CMake to get cross-platform compilation scripts (for VS 9.0 on Windows32 and Makefiles for Unix).

I am experiencing what I cannot understand about add_subdirectory ().

Let me show you my code:

Context:

My architecture for a module named "module1" looks something like this:

  • CMakeLists.txt
  • enable /
    • file1.h
    • file2.h
    • *. H
  • SRC /
    • file1.cpp
    • file2.cpp
    • *. CPP
  • test /
    • CMakeLists.txt
    • SRC /
      • testfile1.cpp
      • testfile2.cpp

The architecture of my entire application consists of these modules, which in themselves are projects that can work independently.

My goals:

  • I want to compile my module as a library

  • I want to test library with code in test / folder

Here is the CMakeLists I wrote:

This file is CMakeLists.txt in the root directory of my module.

#ENSURE MINIMUM VERSION OF CMAKE cmake_minimum_required(VERSION 2.8) #CONFIGURATION OF THE PROJECT #NAME OF THE PROJECT project(MyProject) #OUTPUT OF THE PROJECT set(LIBRARY_OUTPUT_PATH lib/${CMAKE_BUILD_TYPE}) #ADD THE HEADERS OF THE LIBRARY BEING CREATED include_directories(include) #ADD 3rd PARTY OPENCV LIBRARIES find_package(OpenCV REQUIRED) #ADD 3rd PARTY XERCES LIBRARIES include_directories(${XERCES_INCLUDE_DIR}) link_directories(${XERCES_LIB_DIR}) set(Xerces_LIBS xerces-c_3D.lib) #CONFIGURATION OF THE LIBRARY file(GLOB_RECURSE MYPROJECT_MODULE_CXX src/*) file(GLOB_RECURSE MYPROJECT_MODULE_HDR include/*) #NAME OF THE PRESENT LIBRARY set(MYPROJECT_MODULE_LIB_NAME myModuleLib) add_library(${MYPROJECT_MODULE_LIB_NAME} SHARED ${MYPROJECT_MODULE_CXX} ${MYPROJECT_MODULE_HDR} ) target_link_libraries(${MYPROJECT_MODULE_LIB_NAME} ${OpenCV_LIBS} ${Xerces_LIBS} ) #CONTINUE IN THE SUB FOLDERS add_subdirectory(test) 

And then, in the test folder, here is CMakeLists.txt

 #ENSURE MINIMUM VERSION OF CMAKE cmake_minimum_required(VERSION 2.8) #CONFIGURATION OF THE PROJECT #NAME OF THE PROJECT project(MyProjectTest) #OUTPUT OF THE PROJECT set(EXECUTABLE_OUTPUT_PATH bin/${CMAKE_BUILD_TYPE}) #ADD OUR TESTED LIBRARY include_directories(../include) link_directories(../build/lib/${CMAKE_BUILD_TYPE}) #CONFIGURATION OF THE EXE file(GLOB_RECURSE MYPROJECT_MODULE_TEST_CXX src/*) #NAME OF THE PRESENT EXECUTABLE set(MYPROJECT_MODULE_TEST_BIN_NAME myModuleTest) add_executable(${MYPROJECT_MODULE_TEST_BIN_NAME} ${MYPROJECT_MODULE_TEST_CXX} ) target_link_libraries(${MYPROJECT_MODULE_TEST_BIN_NAME} ${MYPROJECT_MODULE_LIB_NAME} ) 

Question

CMake displays the correct solution MyProject.sln Visual Studio 9.0, which successfully compiles in my library related to OpenCV and Xerces (and other libraries of the third part). However, the test binary did not output MyProjectTest.sln.

I thought ( and read in the CMake documentation ) that add_subdirectory (dir) was used to create CMake in the next section (I mean the name could not be clearer: p!), So should I continue to work with CMake in the directory test / and create my solution MyProjectTest.sln?

I use the CMake GUI to run the root CMakeLists.txt in the build directory, which I create in the root of my module. When I examine the assembly directory, where can I find MyProjet.sln, the test / folder, but without MyProjectTest.sln in it!

+2
cmake build-system
Jul 19 '11 at 13:25
source share
2 answers

After three days of trying, I finally found the answer ... Sir DLRdave was actually right: the problem was not in the code itself, but in something "outside the code."

found problem:

I created and edited all my files using Notepad ++. In fact, when opening files with windows notepads (because I was curious) a strange rectangle symbol appeared, and the file did not look like the one I usually see on Notepad ++ I found out that the symbol was "\ n \ r", which Notepad ++ did not show me (it should be filtered), but by going to Windows notepad, you could see that the whole file was "unclean" and appeared on one line instead of the layout that I saw on Notepad ++.

Since this β€œcoding” error appeared only in the CMakeLists subdirectory, it could not be read, but there was no error when interpreting with CMake, and probably why I did not have a returned error from starting CMake.

Decision:

I used the native2ascii.exe tool from Java to fix an encoding error.

Why:

Actually, this probably means that the CMake parser was probably not designed to filter out this type of char appearing with strange encoding, so it gave me 3 days of intensive debugging.

+1
Jul 20 2018-11-21T00:
source share

This may not solve your original problem, but in test/folder/CMakeLists.txt try changing

 #ADD OUR TESTED LIBRARY include_directories(../include) link_directories(../build/lib/${CMAKE_BUILD_TYPE}) 

to

 #ADD OUR TESTED LIBRARY include_directories(${CMAKE_SOURCE_DIR}/include) link_directories(${CMAKE_BINARY_DIR}/lib/${CMAKE_BUILD_TYPE}) 

otherwise, you assume that your build folder is always called build .

+2
Jul 20 '11 at 13:38
source share



All Articles