仅标头库的Visual Studio项目 [英] Visual studio project for header only library
问题描述
我正在创建一个CMake项目,其两个主要文件为:
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)
和
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})
我正在使用它们来生成Visual Studio解决方案,但是我想做的是仅针对页眉生成一个单独的项目
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.
基本上,我有一个小的列表库,通过使用模板,我将其转换为仅标头的库,在进行此类更改之前,我能够生成单独的Visual Studio项目,但在相同的解决方案中,在这种情况下,我可以看到类似这样的内容:
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:
但我想看到的是,假设有可能,这是 ElementaryDataStructures
的单独项目。
But what I'd like to see, assuming this is possible is a separate project for the ElementaryDataStructures
.
我不是CMake和所有设置方面的专家,但是如果您能帮助我弄清楚如何做到这一点,我将非常高兴。
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.
更新:
在评论之后,我得到了VS中的一个新项目,但是还有一点让我感到困扰。
在下面的图片中,我可以看到 alg-and-ds
和 ElementaryDataStructures _
引用了相同的源。有什么方法可以避免 alg-and-ds
项目显示此类文件?
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?
更新 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)
推荐答案
据我所知,没有正常的方法。只是一小撮。因此,您将创建一个自定义目标,该目标将强制MSVC在解决方案树中显示该项目。像这样的东西:
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})
请注意名称中的下划线:可以将其与 add_library $ c $中的名称区分开c>命令。当然,您需要在我的示例中将变量替换为您的实际变量。
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.
这篇关于仅标头库的Visual Studio项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!