简体   繁体   English

cmake和带有源的文件夹

[英]cmake and a folder with the source

-project
--sourсes
--CmakeList.txt

CmakeList.txt to have a repetition: CmakeList.txt有一个重复:

add_executable(Project1 ${SOURCE_FILES}
    sourсes/File1.cpp sourсes/File1.h
    sourсes/File2.cpp sourсes/File2.h
    ...
)

All sources lie in the folder "sourсes", which is not a subproject, modules, etc. 所有源均位于文件夹“sourсes”中,该文件夹不是子项目,模块等。

How to avoid repetition "sourсes" in "add_executable"? 如何避免在“ add_executable”中重复出现“酸”?

I would like to be able to write: 我希望能够写:

add_executable(Project1 ${SOURCE_FILES}
    File1.cpp File1.h
    File2.cpp File2.h
    ...
)

I found this an example: 我发现了一个例子:

add_sources(PREFIX foo
    ROOT_DIR "sources"
        source_one.cpp
        source_two.cpp

    ROOT_DIR "other/sources"
        source_three.cpp
        source_four.cpp
)

add_executable(foo ${foo_SOURCES})

But "add_sources" is unknown. 但是“ add_sources”是未知的。

There is a way to add subdirectories with there own cmakelist.txt. 有一种添加带有自己的cmakelist.txt的子目录的方法。

-project
--CmakeList.txt
--sourсes
---CmakeList.txt

So in your root cmakelist.txt would look like. 因此,在您的根目录下,cmakelist.txt看起来像。

add_subdirectory(source)

The file ./source/cmakelist.txt would look like 文件./source/cmakelist.txt看起来像

set(source_path     "${CMAKE_CURRENT_SOURCE_DIR}")
set(sources
     ${source_path}/File1.cpp
     ${source_path}/File1.h
     ${source_path}/File2.h
     ${source_path}/File2.h
     ...
)

add_executable(foo ${sources})

In this way you could add as many subdirectories as you want without polluting the root cmakelist.txt. 这样,您可以根据需要添加任意数量的子目录,而不会污染根cmakelist.txt。

You could do something like this: 您可以执行以下操作:

set(SOURCES
    File1.cpp File1.h
    File2.cpp File2.h
    ...
)

set(SOURCES_ABS "")

foreach(file IN LISTS SOURCES)
    list(APPEND SOURCES_ABS sources/${file})
endforeach()

add_executable(Project1 ${SOURCES_ABS})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM