2. cmake tutorial 2 - binary target (library)

Program Lang./cmake 2021. 1. 12. 02:11

1. 소스 트리

 

tutorial2 핵심은 myhellow.cpp와 myhellow.hpp 파일을 이용해서 library라는 target을 만드는 것이다.

생성된 library를 실행파일에 최종적으로 연결까지 해 본다.

 

2. CMakeLists.txt 파일

 

앞 장의 basic 편에서 다루었던 부분에 추가된 부분만 설명한다.

add_library는 binary library를 생성하는 명령어이다.

  • add_library
    • myhellow는 생성될 library의 이름이며, 뒤 이어지는 것은 소스 파일들이다.
    • option으로는 STATIC, MODULE, SHARED가 있으며, default는 STATIC 이다.
  • target_link_libraries
    • 실행파일 (Target이라 한다)에 링크될 library를 지정하는 명령어이다.
    • ${PROJECT_NAME}은 실행파일인 target이름이며, 뒤 이어지는 것은 링크될 library 이름이다.
    • 옵션으로는 PRIVATE/PUBLIC/INTERFACE가 있다.
    • cmake.org/cmake/help/latest/command/target_link_libraries.html

 

3. tutorial2.cpp 파일

 

테스트를 위한 기본적인 파일이다.

 

 

4. cmake 실행 결과

 

Out 디렉토리에서 cmake .. 을 수행하면, static library가 생성될 것이다. 이 상태에서 shared library를 생성하고자 하면

Out 디렉토리에서 cmake -D BUILD_SHARED_LIBS=TRUE ../ 을 수행하면 된다.

 

5. git repository

 

아래 개인 git repository에 정리한다.

 

github.com/heesoon/cmake_tutorials/tree/main/tutorial2

'Program Lang. > cmake' 카테고리의 다른 글

1. cmake tutorial 1 - binary target (executable)  (0) 2021.01.10
: