Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmake使用 #11

Open
hujianglang opened this issue Jul 14, 2022 · 0 comments
Open

cmake使用 #11

hujianglang opened this issue Jul 14, 2022 · 0 comments

Comments

@hujianglang
Copy link
Owner

参考:https://github.com/richardchien/modern-cmake-by-example

Modern CMake by Example:

hello:main.cpp
$(CXX) -o hello main.cpp
echo "OK"
注释:
target:hello

make CC=gcc CXX=g++ clean
make CC=gcc CXX=g++ answer

example1:
CMakeLists.txt:

cmake_minimum_required(VERSION 3.9)
project(answer)
set(WOLFRAM_APPID
"" CACHE STRING "WolframAlpha APPID")

if(WOLFRAM_APPID STREQUAL "")
message(SEND_ERROR "WOLFRAM_APPID must not be empty")

endif()

#添加answer子目录
add_subdirectory(answer)
add_subdirectory(curl__wrapper)

target_compile_definitions(libanswer PRIVATE WOLFRAM_APPID="${WOLFRAM_APPID}")

#添加库目标,static指定为静态库
add_library(libanswer STATIC answer.cpp)

message(STATUS "current source dir: ${CMAKE_CURRENT_SOURCE_DIR}")
target_include_directories(libanswer PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)

target_link_libraries()

target_link_libraries(libanswer PRIVATE CURL::libcurl)
add_executable(answer_app main.cpp)
#为answer可执行目标链接libanswer
#target_link_libraries(answer_app libanswer)

if(BUILD_TESTING)
add_subdirectory(tests)
endif()

其他:
cmake -B build#生成构建目录
cmake --build build #执行构建
./build/answer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant