• C++-Cmake指令:add_executable


    • 命令格式

      • add_executable ( [WIN32] [MACOSX_BUNDLE]
              [EXCLUDE_FROM_ALL]
              [source1] [source2 ...])
      • add_executable ( IMPORTED [GLOBAL])
      • add_executable ( ALIAS )

        使用指定的源文件来生成目标可执行文件。这里的目标可执行文件分为三类:普通可执行目标文件导入可执行目标文件别名可执行目标文件。分别对应上面的三种命令格式。

    • 命令解析

      1. 普通可执行目标文件

      add_executable ( [WIN32] [MACOSX_BUNDLE]
            [EXCLUDE_FROM_ALL]
            [source1] [source2 ...])

      通过指定的源文件列表构建出可执行目标文件。

      • name:可执行目标文件的名字,在一个cmake工程中,这个名字必须全局唯一。
      • WIN32:用于windows系统下创建一个以WinMain为入口的可执行目标文件(通常入口函数为main),它不是一个控制台应用程序,而是一个GUI应用程序。当WIN32选项使用的时候,可执行目标的 WIN32_EXECUTABLE会被置位ON
      • MACOSX_BUNDLE:用于mac系统或者IOS系统下创建一个GUI可执行应用程序,当MACOSX_BUNDLE选项使用的时候,可执行目标的MACOSX_BUNDLE会被置位ON
      • EXCLUDE_FROM_ALL:用于指定可执行目标是否会被构建,当该选项使用的时候,可执行目标不会被构建。
      • [source1] [source2 ...]:构建可执行目标文件所需要的源文件。也可以通过target_sources()继续为可执行目标文件添加源文件,要求是在调用target_sources之前,可执行目标文件必须已经通过add_executableadd_library定义了。

      一个例子:

      1. #CMakeLists.txt
      2. cmake_minimum_required(VERSION 3.10.2)
      3. project(test)
      4. SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY output) #设置可执行目标文件的输出目录
      5. include_directories(sub)
      6. add_subdirectory(sub)
      7. add_executable(runtest main.cpp)
      8. target_sources(runtest test.cpp)
      1. // test.h
      2. #include <string>
      3. void test(std::string str);
      4. // test.cpp
      5. #include "test.h"
      6. #include <iostream>
      7. void test(std::string str)
      8. {
      9. std::cout << "In test: " << str << std::endl;
      10. }
      11. // main.cpp
      12. #include "test.h"
      13. int main(int argc, char **argv)
      14. {
      15. test("hello, world!");
      16. return 0;
      17. }
      1. # 执行cmake .;make;./output/runtest后的输出
      2. In test: hello, world!

      2. 导入可执行目标文件

      add_executable ( IMPORTED [GLOBAL])
      将工程外部的可执行目标文件导入进来,不会有任何构建可执行目标文件的动作发生。如果不指定GLOBAL,则可执行目标文件的范围为文件创建的目录及子目录;指定GLOBAL则会将范围扩大到整个工程。IMPORTED选项指定后,属性IMPORTED会被置为TRUE,在工程内构建的可执行目标文件的属性IMPORTED会被置为FALSE

      例如,将外部的git导入到当前工程中:

      1. #CMakeLists.txt
      2. cmake_minimum_required(VERSION 3.10.2)
      3. project(test)
      4. set(GIT_EXECUTABLE "/usr/local/bin/git")
      5. add_executable(Git::Git IMPORTED)
      6. set_property(TARGET Git::Git PROPERTY IMPORTED_LOCATION "${GIT_EXECUTABLE}")
      7. get_target_property(git_location Git::Git IMPORTED_LOCATION)
      8. get_target_property(git_imported Git::Git IMPORTED)
      9. message(">>> git location: ${git_location}, ${git_imported}")
      1. # 输出
      2. >>> git location: /usr/local/bin/git, TRUE

      3. 别名可执行目标文件

      add_executable ( ALIAS )
      为可执行目标文件创建一个别名。创建该别名后,可以使用别名进行可执行目标的读、测试操作,但是不能利用别名对可执行目标的修改属性操作。

      1. #CMakeLists.txt
      2. cmake_minimum_required(VERSION 3.10.2)
      3. project(test)
      4. SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY output)
      5. add_executable(runtest main.cpp)
      6. add_executable(test_name ALIAS runtest)
      7. get_target_property(alias_name test_name ALIASED_TARGET)
      8. if(alias_name)
      9. message(">>> The name test_name is an ALIAS for ${alias_name}")
      10. endif()
      1. # 输出
      2. >>> The name test_name is an ALIAS for runtest

     Cmake命令之add_executable介绍 - 简书

  • 相关阅读:
    星戈瑞FITC-PEG-SH的化学结构和组成
    这些好用的设计网站,你一定要收藏
    flink udtaf 常年不能用
    TypeScript学习日志-第三十二天(infer关键字)
    新手使用 go channel 需要注意的问题
    ZFS了解
    android 多屏幕显示activity,副屏,无线投屏
    护网行动为什么给的钱那么多
    JS -正则表达式
    Espent环境配置与实践
  • 原文地址:https://blog.csdn.net/u013250861/article/details/127936284