CMake add_subdirectory() [英] CMake add_subdirectory()

查看:2225
本文介绍了CMake add_subdirectory()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简介:



我想使用CMake获取跨平台编译脚本(适用于Windows Vista上的VS 9.0和Unix上的Makefiles )。



我遇到了一些我不能理解的关于add_subdirectory()的东西。



我的代码:



上下文:



是这样的:




  • CMakeLists.txt

  • include /

    • file1.h

    • file2.h

    • *。h


  • src /

    • file1.cpp

    • file2.cpp

    • *。cpp


  • test /

    • txt

    • src /

      • testfile1.cpp

      • testfile2.cpp








我的目标:


  1. 我想将我的模块编译为库


  2. 我想使用代码测试库测试/文件夹


这是我写的CMakeLists:



这是我模块根目录中的CMakeLists.txt。

  #ENSURE CMAKE的最小版本
cmake_minimum_required(VERSION 2.8)

#项目配置
项目名称
项目)

项目的输出
set(LIBRARY_OUTPUT_PATH lib / $ {CMAKE_BUILD_TYPE})

#ADD创建的库的头文件
include_directories (包括)

#ADD第三方OPENCV库
find_package(OpenCV REQUIRED)

#ADD第三方XERCES库
include_directories($ {XERCES_INCLUDE_DIR} )
link_directories($ {XERCES_LIB_DIR})
set(Xerces_LIBS xerces-c_3D.lib)


#CONFIGURATION of LIBRARY
文件(GLOB_RECURSE MYPROJECT_MODULE_CXX src / *)
文件(GLOB_RECURSE MYPROJECT_MODULE_HDR包含/ *)

现有库的#NAME
设置(MYPROJECT_MODULE_LIB_NAME myModuleLib)
add_library($ {MYPROJECT_MODULE_LIB_NAME}
SHARED
$ {MYPROJECT_MODULE_CXX}
$ {MYPROJECT_MODULE_HDR}

target_link_libaries($ {MYPROJECT_MODULE_LIB_NAME}
$ {OpenCV_LIBS}
$ {Xerces_LIBS }


在子文件夹中输入#CONTINUE
add_subdirectory(test)

然后,在测试/文件夹中,这里是CMakeLists.txt

  #ENSURE CMAKE的最低版本
cmake_minimum_required(VERSION 2.8)

#项目配置
项目名称
项目(MyProjectTest)

项目的OUTPUT
设置(EXECUTABLE_OUTPUT_PATH bin / $ {CMAKE_BUILD_TYPE})

#ADD我们测试的库
include_directories (../include)
link_directories(../ build / lib / $ {CMAKE_BUILD_TYPE})


#CONFIGURATION的EXE
文件(GLOB_RECURSE MYPROJECT_MODULE_TEST_CXX src / *)

#目前可执行的
集(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}

问题



CMake输出一个正确的MyProject.sln Visual Studio 9.0解决方案,它在我的库中与OpenCV和Xerces其他第三方库)。但是测试二进制没有输出任何MyProjectTest.sln。



我想,(并读入CMake文档)add_subdirectory(dir)用于在以下子目录中执行CMake我的意思是,名字不能更清楚:p!),所以不应该继续CMake在测试/目录,并创建我的MyProjectTest.sln解决方案?



我使用GUI CMake运行根目录CMakeLists.txt在一个构建目录,我创建在我的模块的根。当我探索构建目录,我可以找到我的MyProjet.sln,一个测试/文件夹,但没有MyProjectTest.sln在其中!

解决方案

经过三天的尝试,我终于找到了答案... DLRdave先生是对的:问题不是来自代码本身,而是来自代码



问题发现:



我用Notepad ++创建和编辑了所有文件。实际上当打开文件与Windows记事本(因为我很好奇)一个奇怪的矩形符号出现,该文件看起来不像我通常看到在记事本++
我发现,符号是一个\\\
\\ \\ rNotepad ++没有显示我(它必须被过滤),但是在Windows记事本,你可以看到整个文件是不纯,出现在一行而不是在Notepad ++看到的布局。



因为这个编码错误只出现在子目录CMakeLists,它不能读,但说没有错误,当解释与CMake,这可能是为什么我没有返回的错误运行CMake 。



我使用了来自Java的native2ascii.exe工具修正编码错误。



原因:



是CMake的语法解析器可能没有被设计为过滤这种类型的字符出现与奇怪的编码,这就是为什么它给我3天的激烈的调试。


Introduction:

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

I am experiencing something i can't understand about add_subdirectory().

Let me show you my code :

Context:

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

  • CMakeLists.txt
  • include/
    • file1.h
    • file2.h
    • *.h
  • src/
    • file1.cpp
    • file2.cpp
    • *.cpp
  • test/
    • CMakeLists.txt
    • src/
      • testfile1.cpp
      • testfile2.cpp

The architecture of my whole application is composed of these modules which are in themselves projects that could work independantly.

My goals:

  1. I want to compile my module as a library

  2. I want to test the library with the code in the test/ folder

Here are the CMakeLists i wrote :

This one is the 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 the 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

The CMake outputs a correct MyProject.sln Visual Studio 9.0 solution, which compiles successfully in my library linked with OpenCV and Xerces (and other 3rd part libraries). However the test binary did not output any MyProjectTest.sln.

I thought, (and read in the CMake documentation) that add_subdirectory(dir) was used to do CMake in the following sub directory (i mean, the name could not be clearer :p !), so shouldn't it continue CMake in the test/ directory and create my MyProjectTest.sln solution ?

I use the GUI CMake to run the root CMakeLists.txt in a build directory that i create in the root of my module. When I explore the build directory that's where I can find my MyProjet.sln, a test/ folder, but no MyProjectTest.sln in it !

解决方案

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

Problem found:

I created and edited all my files with Notepad++. Actually when opening the files with windows notepad (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 a "\n\r" that Notepad++ did not show me (it must be filtered) but going on windows notepad, you could see that the whole file was "impure" and appeared on a single line instead of the layout i saw on Notepad++.

As this "encoding" bug appeared only in the subdirectory CMakeLists, it could not be read but said no error when interpreting with CMake and that's probably why I had no returned error from running CMake.

Solution:

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

The why:

Actually what it probably means is that the syntaxic parser of CMake has probably not been designed for filtering this type of char appearing with strange encoding, that's why it gave me 3 days of intense debugging.

这篇关于CMake add_subdirectory()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆