0

I am new to cmake file creation. I have c++ code in list of directory, each directory has its own CMakeLists.txt file. For compile and create executable in each directory, I have to go inside directory and execute following two command on terminal.

$ cmake .
$ make

Example directory structure: enter image description here

Each directory 1, 2 ...41 has its own `CMakeLists.txt'.

Example inside each directory file structure: enter image description here

For compile and create executable in each directory, i have to follow those tow command.

But now i have to write either python script/ cmake script to do above this action in each directory and capture the output and store in log file.

Kindly anyone to guide me to solve my issue. Thanks in advance.

My each directory CMakeLists.txt like this:

project(foundations)
cmake_minimum_required(VERSION 2.6)

include_directories($ENV{GMOCK_HOME}/include $ENV{GMOCK_HOME}/gtest/include)
link_directories($ENV{GMOCK_HOME}/mybuild $ENV{GMOCK_HOME}/gtest/mybuild)
add_definitions(-std=c++11)

set(sources main.cpp RetweetCollectionTest.cpp)
add_executable(test ${sources})
target_link_libraries(test pthread)
target_link_libraries(test gmock)
target_link_libraries(test gtest)
nagarajan
  • 474
  • 3
  • 21

1 Answers1

3

add_subdirectory https://cmake.org/cmake/help/v3.0/command/add_subdirectory.html is exactly what you need.

EDIT:

For example:

add_subdirectory(1 1)

Where the first parameter is source dir, and the second is binary dir. So, the names should not overlap.

Also, check this: https://stackoverflow.com/a/7788165/934691

But, if you don't want to rewrite your CMakeLists, a simple bash script like

for dir in ls ; do cd $dir; cmake . && make; cd ..; done

will be more appropriate for you.

Community
  • 1
  • 1
  • I already tried with `add_subdirectory(1)`.. and so. But when run cmake for this root file. It shows following error `add_executable` cannot create target "test" because another target same already exists. – nagarajan Mar 22 '16 at 19:21
  • Did you try setting `binary_dir` parameter? –  Mar 22 '16 at 19:23
  • which directory i have to set for that. Can you write the cmake for my requirement, so that i can understand little more – nagarajan Mar 22 '16 at 19:24