{"id":3915,"date":"2019-10-13T01:08:32","date_gmt":"2019-10-12T17:08:32","guid":{"rendered":"https:\/\/damogame.cn\/wordpress\/?p=3915"},"modified":"2019-10-13T01:08:32","modified_gmt":"2019-10-12T17:08:32","slug":"%e4%b8%80%e4%b8%aa%e5%80%bc%e5%be%97%e5%ad%a6%e4%b9%a0%e7%9a%84cmake%e6%96%87%e4%bb%b6%e5%86%99%e6%b3%95","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/3915","title":{"rendered":"\u4e00\u4e2a\u503c\u5f97\u5b66\u4e60\u7684cmake\u6587\u4ef6\u5199\u6cd5"},"content":{"rendered":"<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">cmake_minimum_required(VERSION 2.8)\r\n\r\n# Options. Turn on with 'cmake -Dmyvarname=ON'.\r\noption(test \"Build all tests.\" OFF) # Makes boolean 'test' available.\r\n\r\n# Make PROJECT_SOURCE_DIR, PROJECT_BINARY_DIR, and PROJECT_NAME available.\r\nset(PROJECT_NAME MyProject)\r\nproject(${PROJECT_NAME})\r\n\r\nset(CMAKE_CXX_FLAGS \"-g -Wall\")\r\n\r\n# force static runtime libraries for msvc builds\r\nif(MSVC)\r\n  set(variables \r\n    CMAKE_CXX_FLAGS_DEBUG\r\n    CMAKE_CXX_FLAGS_RELEASE\r\n    CMAKE_CXX_FLAGS_RELWITHDEBINFO\r\n    CMAKE_CXX_FLAGS_MINSIZEREL\r\n  )\r\n  foreach(variable ${variables})\r\n    if(${variable} MATCHES \"\/MD\")\r\n      string(REGEX REPLACE \"\/MD\" \"\/MT\" ${variable} \"${${variable}}\")\r\n    endif()\r\n  endforeach()\r\nendif()\r\n\r\n# If you want your own include\/ directory, set this, and then you can do\r\n# include_directories(${COMMON_INCLUDES}) in other CMakeLists.txt files.\r\n# set(COMMON_INCLUDES ${PROJECT_SOURCE_DIR}\/include)\r\n\r\n################################\r\n# Normal Libraries &amp; Executables\r\n################################\r\nadd_library(project1_lib project1.cpp)\r\nadd_executable(project1 main.cpp)\r\n# Key idea: SEPARATE OUT your main() function into its own file so it can be its\r\n# own executable. Separating out main() means you can add this library to be\r\n# used elsewhere.\r\n\r\ntarget_link_libraries(project1 project1_lib)\r\n\r\n################################\r\n# Testing\r\n################################\r\nif (test)\r\n  if (APPLE)\r\n    add_definitions(-DGTEST_USE_OWN_TR1_TUPLE)\r\n    add_definitions(-D__GLIBCXX__)\r\n  endif (APPLE)\r\n\r\n  # This adds another subdirectory, which has 'project(gtest)'.\r\n  add_subdirectory(lib\/gtest-1.6.0)\r\n\r\n  enable_testing()\r\n\r\n  # Include the gtest library. gtest_SOURCE_DIR is available due to\r\n  # 'project(gtest)' above.\r\n  include_directories(${gtest_SOURCE_DIR}\/include ${gtest_SOURCE_DIR})\r\n\r\n  ##############\r\n  # Unit Tests\r\n  ##############\r\n  add_executable(runUnitTests test_project1.cpp)\r\n\r\n  # Standard linking to gtest stuff.\r\n  target_link_libraries(runUnitTests gtest gtest_main)\r\n\r\n  # Extra linking for the project.\r\n  target_link_libraries(runUnitTests project1_lib)\r\n\r\n  # This is so you can do 'make test' to see all your tests run, instead of\r\n  # manually running the executable runUnitTests to see those specific tests.\r\n  add_test(NAME that-test-I-made COMMAND runUnitTests)\r\n\r\n  # You can also omit NAME and COMMAND. The second argument could be some other\r\n  # test executable.\r\n  add_test(that-other-test-I-made runUnitTests)\r\nendif()<\/pre>\n<p>&nbsp;<\/p>\n<p>\u6765\u81ea\uff1a<\/p>\n<p><a href=\"https:\/\/github.com\/dmonopoly\/gtest-cmake-example\">https:\/\/github.com\/dmonopoly\/gtest-cmake-example<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>cmake_minimum_requir<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[118],"class_list":["post-3915","post","type-post","status-publish","format-standard","hentry","tag-02-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/3915","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=3915"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/3915\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=3915"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=3915"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=3915"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}