Visual studio project for header only library

2020-03-07 07:02发布

I'm creating a CMake project whose two main files are:

cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
project(CPP_Algorithms_and_Data_Structures)

set( CMAKE_CXX_STANDARD 11 )

#add_subdirectory(./ElementaryAlgorithms)
add_subdirectory(./ElementaryDataStructures)
#add_subdirectory(./AdvancedDataStructures)
#add_subdirectory(./GraphAlgorithms)

#set(INCLUDE_FOLDERS 
#   ./
#   ./ElementaryAlgorithms 
#   ./ElementaryDataStructures 
#   ./AdvancedDataStructures 
#   ./GraphAlgorithms)

set(INCLUDE_FOLDERS ./ ./ElementaryDataStructures)

set(HEADER_FILES alg-and-ds.h)
set(SRC_FILES main.cpp alg-and-ds.cpp)

add_executable(alg-and-ds ${SRC_FILES} ${HEADER_FILES})
target_include_directories(alg-and-ds PUBLIC ${INCLUDE_FOLDERS})
target_link_libraries(alg-and-ds elementary-data-structures)

#target_link_libraries(alg-and-ds
#   graph-algorithms
#   elementary-data-structures
#   elementary-algorithms
#   advanced-data-structures)

and

cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
project(ElementaryDataStructures)

set( CMAKE_CXX_STANDARD 11 )

if(WIN32)
    set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS YES CACHE BOOL "Export all symbols")
endif()

add_library(elementary-data-structures INTERFACE)
target_include_directories(elementary-data-structures INTERFACE ./)
target_sources(elementary-data-structures INTERFACE 
    "${CMAKE_CURRENT_LIST_DIR}/list.h"
    "${CMAKE_CURRENT_LIST_DIR}/list.tcc")
#set_target_properties(elementary-data-structures PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})

I'm using these to generate a visual studio solution, however what I would like to do is to generate a separate project for the header only library.

Basically I had a small list library that I converted to an header only library, by using templates, before such change I was able to generate separate visual studio projects but in the same solution, in this case instead I can see something like this:

enter image description here

But what I'd like to see, assuming this is possible is a separate project for the ElementaryDataStructures.

I'm not an expert in CMake and all the setups, but I would be great if you could help me to figure out how to do it.

Update:

Following suggestion on the comment I got a new project in VS, however there's still a tiny bit that bothers me. In the picture below I can see both alg-and-ds and ElementaryDataStructures_ referencing the same sources. Is there a way to avoid the alg-and-ds project to show such files?

The update CMakeLists.txt

cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
project(ElementaryDataStructures)

set( CMAKE_CXX_STANDARD 11 )

if(WIN32)
    set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS YES CACHE BOOL "Export all symbols")
endif()

add_library(elementary-data-structures INTERFACE)
target_include_directories(elementary-data-structures INTERFACE ./)
target_sources(elementary-data-structures INTERFACE 
    "${CMAKE_CURRENT_LIST_DIR}/list.h"
    "${CMAKE_CURRENT_LIST_DIR}/list.tcc")
add_custom_target(ElementaryDataStructures_ SOURCES ${CMAKE_CURRENT_LIST_DIR}/list.h ${CMAKE_CURRENT_LIST_DIR}/list.tcc)

enter image description here

标签: c++ cmake
2条回答
smile是对你的礼貌
2楼-- · 2020-03-07 07:41

As far as I know there is no normal way to do it. Only a hackish one. So you create a custom target which will force MSVC to show the project in the solution tree. Something like this:

add_custom_target(${PROJECT_NAME}_ SOURCES ${PROJECT_SOURCES})

Note the underscore in the name: it is there to differentiate it from the name in the add_library command. Of course you need to replace the variables in my example to yours actual ones.

查看更多
放荡不羁爱自由
3楼-- · 2020-03-07 07:43

Another solution is to declare static library with stub source file:

file(TOUCH ${CMAKE_BINARY_DIR}/stub.cpp)

add_library(elementary-data-structures STATIC
    "${CMAKE_BINARY_DIR}/stub.cpp"
    "${CMAKE_CURRENT_LIST_DIR}/list.h"
    "${CMAKE_CURRENT_LIST_DIR}/list.tcc"
)
target_include_directories(elementary-data-structures INTERFACE ./)
查看更多
登录 后发表回答