Skip to content

Latest commit

 

History

History
156 lines (114 loc) · 2.77 KB

README.3.md

File metadata and controls

156 lines (114 loc) · 2.77 KB

03 Multiple targets

That's cool, but we had to clean the build directory. Can we build all at once?

create top-level CMakeLists.txt

# ~/tutorials/CMakeLists.txt

cmake_minimum_required(VERSION 3.0)

project(build_all CXX)

add_subdirectory(test_hello_world)
add_subdirectory(test_factorial)

build all targets

clean build folder

cd ~/tutorials/build
rm -r *

generate makefiles

cmake ..
-- The CXX compiler identification is GNU 4.8.5
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/marehr/tutorials/build

make binaries

make
Scanning dependencies of target hello
[ 50%] Building CXX object test_hello_world/CMakeFiles/hello.dir/hello.cpp.o
Linking CXX executable hello
[ 50%] Built target hello
Scanning dependencies of target factorial
[100%] Building CXX object test_factorial/CMakeFiles/factorial.dir/factorial_main.cpp.o
Linking CXX executable factorial
[100%] Built target factorial

building all binaries takes to long? you can also cherry-pick.

make factorial
[100%] Built target factorial

Where are the binaries?

cd ~/tutorials/build/
ls ~/tutorials/build/
CMakeCache.txt  CMakeFiles  cmake_install.cmake  Makefile  test_factorial  test_hello_world

They are in their respective folder.

test_factorial/factorial
test_hello_world/hello
120 = factorial(6)
Hello World

Change binaries folder.

One folder would be nicer. So change the top-level CMakeLists.txt

# ~/tutorials/CMakeLists.txt

cmake_minimum_required(VERSION 3.0)

project(build_all CXX)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

add_subdirectory(test_hello_world)
add_subdirectory(test_factorial)

rebuild the sources

cd ~/tutorials/build
make
-- Configuring done
-- Generating done
-- Build files have been written to: /home/marehr/tutorials/build
Linking CXX executable ../bin/hello
[ 50%] Built target hello
Linking CXX executable ../bin/factorial
[100%] Built target factorial

The source are now in ~/tutorials/build/bin.

ls ~/tutorials/build/bin
factorial  hello

Executing the binaries

bin/factorial
bin/hello
120 = factorial(6)
Hello World

prev · next