CMake基本用法

发布于 2024-11-05 16:56:09 字数 693 浏览 1 评论 0原文

我正在尝试使用 CMake 构建我的库。我正在处理 CMakeLists.txt 。

我希望能够执行以下操作:

我有一个名为“然后”的目录

include
src

,其中有 阿吉文件夹。 该文件夹中是库的子文件夹。

到目前为止,我收集到的信息是:

我需要做:

set(AGUI_SOURCES
src/Agui/Rectangle.cpp
src/Agui/xxx.cpp (and so on)
)

然后我认为需要做:

include_directories(./include)

然后我不太确定。

我知道 add_library 会参与其中,但我不确定如何参与。

问题是,我想创建 2 个选项:DLL 或静态。

如果是DLL,则必须定义AGUI_BUILD_DLL。

因此,根据这些信息:

  1. 我走在正确的道路上吗?
  2. 如何创建选项
  3. add_library 中应放入什么
  4. 如果用户想要 DLL 版本,如何添加预处理器 AGUI_BUILD_DLL?

如果其中任何一个或所有这些问题都能得到解答,我将不胜感激。

谢谢

I'm trying to build my library with CMake. I'm working on CMakeLists.txt .

I want to be able to do the following:

I have a directories called

include
src

Then inside of these there is
Agui folder.
And in that folder are the sub folders of the library.

So far from what I've gathered:

I'll need to do:

set(AGUI_SOURCES
src/Agui/Rectangle.cpp
src/Agui/xxx.cpp (and so on)
)

Then I think need to do:

include_directories(./include)

And then I'm not quite sure.

I know that add_library will be involved but I'm not sure how.

The thing is, I want to create 2 options: DLL, or static.

If it is DLL, then AGUI_BUILD_DLL must be defined.

So based on this information:

  1. Am I on the right track?
  2. How do I create the options
  3. What do I put in add_library
  4. How do I add the preprocessor AGUI_BUILD_DLL if the user wants the DLL version?

If any or all of these could be answered I would greatly appreciate it.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梅倚清风 2024-11-12 16:56:09

我建议您查看 CMake 教程,其中介绍了添加可执行文件、库、系统自省等。 cmake 命令也是自记录的,因此在命令行输入时,

cmake --help-command add_library

将为您提供 add_library 命令的文档。您可以使用 --help-commands 获取所有 CMake 命令文档的完整列表。因此,您可以使用 option 命令添加用于构建共享或静态的选项,并使用 add_definitions 命令添加预处理器定义。

例如,

option(BUILD_SHARED "Build shared libraries" OFF)
if(BUILD_SHARED)
  add_definitions(-DAGUI_BUILD_DLL)
  add_library(agui SHARED ${AGUI_SOURCES})
else()
  add_library(agui STATIC ${AGUI_SOURCES})
endif()

您应该注意 targetName_EXPORTS 将在构建库时定义,因此可以在 declspec 逻辑中使用。这是由 CMake 完成的,在 Unix 系统上的 GCC 中使用可见性支持时也很有用。

I would recommend taking a look at the CMake tutorial which covers adding executables, libraries, system introspection etc. The cmake command also self documents, so on the command line entering,

cmake --help-command add_library

would give you the documentation for the add_library command. You could use the --help-commands to get a full listing of all CMake command documentation. So you can use the option command to add an option for building shared or static, and use the add_definitions command to add a preprocessor definition.

For example,

option(BUILD_SHARED "Build shared libraries" OFF)
if(BUILD_SHARED)
  add_definitions(-DAGUI_BUILD_DLL)
  add_library(agui SHARED ${AGUI_SOURCES})
else()
  add_library(agui STATIC ${AGUI_SOURCES})
endif()

You should note targetName_EXPORTS will be defined when building the library, and so can be used in the declspec logic. This is done by CMake, and is also useful when using visibility support in GCC on Unix systems.

遥远的她 2024-11-12 16:56:09
  1. 也许吧,不知道:)
  2. 选项(AGUI_BUILD_DLL“构建agui DLL”关闭)
    靠近 CMakeLists.txt 开头的某个位置。您可能想要使用 ON 而不是 OFF - 取决于您是否希望 DLL 构建为默认值。

3,4。

if(AGUI_BUILD_DLL)
  add_library(agui SHARED ${AGUI_SOURCES})
  set_target_properties(agui PROPERTIES DEFINE_SYMBOL "AGUI_BUILD_DLL")
else()
  add_library(agui STATIC ${AGUI_SOURCES})
endif()
  1. Maybe , do not know :)
  2. OPTION(AGUI_BUILD_DLL "Build agui DLL" OFF)
    somewhere near the start of CMakeLists.txt. You may want to use ON instead of OFF - depends on whether you want DLL build to de default.

3,4.

if(AGUI_BUILD_DLL)
  add_library(agui SHARED ${AGUI_SOURCES})
  set_target_properties(agui PROPERTIES DEFINE_SYMBOL "AGUI_BUILD_DLL")
else()
  add_library(agui STATIC ${AGUI_SOURCES})
endif()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文