当前位置: 首页 > 工具软件 > ios-cmake > 使用案例 >

C++-Cmake指令:add_executable

湛钊
2023-12-01
  • 命令格式

    • add_executable (<name> [WIN32] [MACOSX_BUNDLE]
            [EXCLUDE_FROM_ALL]
            [source1] [source2 ...])
    • add_executable (<name> IMPORTED [GLOBAL])
    • add_executable (<name> ALIAS <target>)

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

  • 命令解析

    1. 普通可执行目标文件

    add_executable (<name> [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定义了。

    一个例子:

     #CMakeLists.txt
     cmake_minimum_required(VERSION 3.10.2)
     project(test)
    
     SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY output) #设置可执行目标文件的输出目录
    
     include_directories(sub)
     add_subdirectory(sub)
    
     add_executable(runtest main.cpp)
     target_sources(runtest test.cpp)
    
    // test.h
    #include <string>
    void test(std::string str);
    
    // test.cpp
    #include "test.h"
    #include <iostream>
    void test(std::string str)
    {
        std::cout << "In test: " << str << std::endl;
    }
    
    // main.cpp
    #include "test.h"
    int main(int argc, char **argv)
    {
        test("hello, world!");
        return 0;  
    }
    
    # 执行cmake .;make;./output/runtest后的输出
    In test: hello, world!
    

    2. 导入可执行目标文件

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

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

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

    3. 别名可执行目标文件

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

    #CMakeLists.txt
    cmake_minimum_required(VERSION 3.10.2)
    project(test)
    
    SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY output)
    
    add_executable(runtest main.cpp)
    
    add_executable(test_name ALIAS runtest)
    get_target_property(alias_name test_name ALIASED_TARGET)
    if(alias_name)
        message(">>> The name test_name is an ALIAS for ${alias_name}")
    endif()
    
    # 输出
    >>> The name test_name is an ALIAS for runtest
    

 Cmake命令之add_executable介绍 - 简书

 类似资料: