当前位置:网站首页>Cmake common command category notes

Cmake common command category notes

2022-06-22 02:11:00 Leaves that go to bed early

1. Set the path of the header file

target_include_directories(Tutorial PUBLIC
                           "${PROJECT_BINARY_DIR}"
                           )
#  Link header file path 
target_include_directories(MathFunctions
          INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
          )

2. Setup profile

configure_file(TutorialConfig.h.in TutorialConfig.h)

TutorialConfig.h.in :

// the configured options and settings for Tutorial
#define Tutorial_VERSION_MAJOR @[email protected]
#define Tutorial_VERSION_MINOR @[email protected]

Access profile :

  if (argc < 2) {
    
    // report version
    std::cout << argv[0] << " Version " << Tutorial_VERSION_MAJOR << "."
              << Tutorial_VERSION_MINOR << std::endl;
    std::cout << "Usage: " << argv[0] << " number" << std::endl;
    return 1;
  }

3. Set up c/c++ edition

# specify the C++ standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)

4. Compile instructions

cd Step1_build
cmake --build .

5. Link to third-party libraries

target_link_libraries(Tutorial PUBLIC MathFunctions)

add_library(hello::library ALIAS hello_library)

6. Macro control

#  Set a variable 
option(USE_MYMATH "Use tutorial provided math implementation" ON)

#  Macro control 
if(USE_MYMATH)
  add_subdirectory(MathFunctions)
  list(APPEND EXTRA_LIBS MathFunctions)
  list(APPEND EXTRA_INCLUDES "${PROJECT_SOURCE_DIR}/MathFunctions")
endif()


#  Set the number 
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")

7. Search for files

#  Find all the source files in the directory 
#  And save the name to  DIR_SRCS  Variable 
aux_source_directory(. DIR_SRCS)

aux_source_directory(<dir> <variable>)

8. Compile result type settings

#  Specify generation  MathFunctions  Link library 
add_library (MathFunctions ${DIR_LIB_SRCS})

9. Set a variable

set(SOURCES
    src/Hello.cpp
    src/main.cpp
)

# Add an executable with the above sources
add_executable(hello_headers ${SOURCES})

10. Print a message

# Print path to generated files
message ("PROTO_SRCS = ${PROTO_SRCS}")
message ("PROTO_HDRS = ${PROTO_HDRS}")
原网站

版权声明
本文为[Leaves that go to bed early]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220159465846.html