Martins3 / refresh.nvim

immature and frenzy thoughts
0 stars 0 forks source link

cmake #5

Open Martins3 opened 3 years ago

Martins3 commented 3 years ago

Generate exe for every cpp

project(go)

cmake_minimum_required(VERSION 3.16)

file( GLOB APP_SOURCES *.cpp)
foreach( testsourcefile ${APP_SOURCES} )
    get_filename_component(testname ${testsourcefile} NAME_WE)
    # message(STATUS ${testname})
    add_executable( ${testname} ${testsourcefile} )
endforeach( testsourcefile ${APP_SOURCES} )

https://stackoverflow.com/questions/14306642/adding-multiple-executables-in-cmake

project(fuck)
add_executable(mytool mytool.cpp)
add_executable(fuck fuck.cpp)
Martins3 commented 3 years ago

lib

cmake_minimum_required(VERSION 2.8.4)

set(LIBHELLO_SRC hello.c)

add_library(hello_dynamic SHARED ${LIBHELLO_SRC})
add_library(hello_static STATIC ${LIBHELLO_SRC})

set_target_properties(hello_dynamic PROPERTIES OUTPUT_NAME "hello")
set_target_properties(hello_dynamic PROPERTIES VERSION 1.2 SOVERSION 1)
set_target_properties(hello_static PROPERTIES OUTPUT_NAME "hello")

set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)

install(TARGETS hello_dynamic hello_static
        LIBRARY DESTINATION lib
        ARCHIVE DESTINATION lib)

install(FILES hello.h DESTINATION include/hello)
Martins3 commented 3 years ago

include_directories

add_executable(main main.cpp)

set(CMAKE_INCLUDE_CURRENT_DIR 0)  # bug

message(STATUS ${CMAKE_INCLUDE_CURRENT_DIR})
message(STATUS ${CMAKE_CURRENT_SOURCE_DIR})
message(STATUS ${CMAKE_CURRENT_BINARY_DIR})

# include_directories(../include/hello) # bug

set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
#include <iostream>
#include <fuck.hpp>
#include <hello.hpp>

int main(void) {
  std::cout << "fuck" << std::endl;
  return 0;
}
├── include
│   └── hello
│       └── hello.hpp
└── src
    ├── main.cpp
Martins3 commented 3 years ago

Custom

cmake_minimum_required(VERSION 3.10)
project(foo)

set(TEST_FILE "log.txt")
set(GG_FILE "gg.txt")

# add_custom_command does not create a new target. You have to define targets explicitly
# by add_executable, add_library or add_custom_target in order to make them visible to make
add_custom_command(OUTPUT ${TEST_FILE}
    COMMAND touch ${TEST_FILE}

    # Display the given message before the commands are executed at build time
    COMMENT "Creating ${TEST_FILE}"
    DEPENDS ${GG_FILE}
)

add_custom_command(OUTPUT ${GG_FILE}
    COMMAND touch ${GG_FILE}

    # Display the given message before the commands are executed at build time
    COMMENT "Creating ${GG_FILE}"
)

# target zoo is always built
add_custom_target(zoo ALL
    COMMAND echo "This is ALL target 'zoo', and it depends on ${TEST_FILE}"
    # If the file exists, then commands related to that file won't be executed
    # DONOT let other target depends on the same OUTPUT as current target,
    #   or it may be bad when doing parallel make
    DEPENDS ${TEST_FILE}

    # to make quotes printable,for example
    VERBATIM
)