CMAKE for a build a simple framework

2019-07-23 22:40发布

问题:

I have my mind crashing with cmake. After this answer I have tried to make a simple example and put it in github because there are a lot of file inside directories and could be boring copy everything here.

What I'd like to do is to build a simple frameworks for handling my qt/opencv/opengl experiments. What I like to have is a repository with those directories:

 root*
   |-apps
   |---test1*
   |
   |-build
   |-cmake*
   |
   |-modules
   |---foo*

(The * signed directory are the ones with some cmake files like CmakeLists.txt or FindXXModule.cmake)

In modules i can write the modules (for example a module for face recognition, a module for draw a red cube in opengl, a module that contains my personal qt widget extension).

Than I need an easy way for create an application and link some modules on it. For that I thought to create a cmake directory where to put the FindXXModule.cmake and in the apps just say: find_package(XXModule).

Note that for now I don't care about installing this repository and the tree structure must be this one (so if I am in a apps/test2 I know I can refer to the cmake directory as ../../cmake or the module directory is ../../modules)

I have wrote a little example with the app named test1 that uses the module foo and i put it in a github repository.

For now I can compile the application test1 with cmake calling cmake path_to_test1_CmakeLists.txt and I am happy about that. But if I try to launch cmake path_to_root_CmakeLists.txt it does not work because the file Findfoo.cmake is read two time (and i did't be able to use some if for not reading it twice).

Then, if i run the test1 cmake a foo directory with cmake cache etc are created in root/cmake and I don't want it. I want all file cmake has to generate are in root/build directory.

So, those are my 2 question:

  • How create a CmakeLists.txt that can build all the apps and all the future test i will write in the modules directory
  • How avoid that launching cmake of a single app will create files in the cmake directory.

Sorry if my english and my idea of how cmake works are not good.. i hope it is clear.

EDIT:

One more thing. In Findfoo.cmake I have a bad hack: for adding the real CMakeLists.txt inside a modules/foo when I call the cmake from test1 I have to add a subdirectory that is not in the tree.. Maybe this kind of hack could be deleted reviewing the enteire structure..

回答1:

As you say you want to put the whole directory structure into source control. This means these folder structure is same on every location where you do a checkout. So why creating the Findfoo.cmake if you have a relative path the the foo directory?

I suggest to put a CMakelists.txt file in to root that adds all subdirectories. To reduce confusion between files generated by CMake and original files, you should create a folder called ./build (or even ../build) and run CMake in that directory with the root directory as first argument. This creates all CMake generated files in the ./build directory and gives you the possibility to clean it up easily. This way of working is called out-of-source build and its highly recommended to use cmake in this way. See this question for an example.



标签: cmake