1# CMakeLists.txt for PrimUtil a library for utilities related to working with prime numbers.
cmake_minimum_required(VERSION 2.8.9)
project (PrimeUtil)
set(CMAKE_CXX_STANDARD 11)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
add_library(PrimeUtil SHARED src/primeutil.cpp)
install(TARGETS PrimeUtil DESTINATION /usr/lib)
install(FILES include/primeutil.h DESTINATION include)
2# Make the generator and build from the build directory.
$ cd build
$ cmake ..
-- The C compiler identification is GNU 5.5.0
-- The CXX compiler identification is GNU 5.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- 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/tasuku/workspace/cpp/project_euler/PrimeUtil/build
$ make
Scanning dependencies of target PrimeUtil
[ 50%] Building CXX object CMakeFiles/PrimeUtil.dir/src/primeutil.cpp.o
[100%] Linking CXX shared library libPrimeUtil.so
[100%] Built target PrimeUtil
3# Make the library available system wide. This will install and place in /usr/lib
.
$ sudo make install
[100%] Built target PrimeUtil
Install the project...
-- Install configuration: ""
-- Installing: /usr/lib/libPrimeUtil.so
-- Up-to-date: /usr/local/include/primeutil.h
4# CMakeLists.txt for example program (solution to a Project Euler question) that links to PrimeUtil library.
cmake_minimum_required(VERSION 2.8.9)
project (largest_prime_factor)
set(CMAKE_CXX_STANDARD 11)
find_library(PRIMEUTIL_LIB libPrimeUtil.so)
message(STATUS ${PRIMEUTIL_LIB})
add_executable(largest_prime_factor src/main.cpp)
target_link_libraries(largest_prime_factor PRIVATE ${PRIMEUTIL_LIB})
5# Make the generator and build. A binary called largest_prime_factor
should be found in the /build
and executable.
$ ./largest_prime_factor 5678899999
Largest prime factor is: 6217
References
- https://cmake.org/cmake/help/v3.0/manual/cmake-language.7.html#line-comment
- http://www.dillonbhuff.com/?p=15
- http://derekmolloy.ie/hello-world-introductions-to-cmake/