如何正确链接库与 cmake?
问题描述
我无法将正在使用的其他库正确链接到我的项目中.
我正在使用 CLion,它使用 cmake 来构建它的项目.我正在尝试将几个库与 OpenGL 结合使用来对某些对象进行纹理处理.我最初在 Visual Studio 中构建它,因为我无法弄清楚如何让 cmake 与 Clion 一起工作.但是,既然代码都可以运行(无论如何在 Visual Studio 中),我希望能够使用 CLion,因为这是我的首选 IDE.
我还是 cmake 的新手,我不明白我的 CMakeLists.txt
做错了什么.这是我所拥有的:
我对其进行了调整,直到它在 CLion 中不再给我任何错误为止,但是我的代码中仍然无法识别头文件.
这是我的项目结构:
所以,我把我需要的所有库都放进去了,但它似乎没有在代码中识别它们.Clion 在项目中识别它们(它们不会因错误而显示为红色),但是在构建时(当我尝试在 CLion 中运行它时),我收到以下错误:
基本上,每次使用 SDL 和 glew 都会出错,但不是 glm,这很奇怪.
我的 CMakeLists.txt
有什么问题?
我的建议是从简单开始,然后进一步复杂化您的项目.
让我试着解释一下 CMake 中的链接是如何工作的.这个想法是你在 CMake 中构建模块,并将它们链接在一起.让我们暂时忽略头文件,因为它们都可以包含在您的源文件中.
假设您有 file1.cpp、file2.cpp、main.cpp.您可以通过以下方式将它们添加到您的项目中:
现在您将它们添加到名为 LibsModule
的模块中.记住这一点.假设您想链接到 pthread
例如,它已经在系统中.您可以使用以下命令将其与 LibsModule
结合使用:
如果你也想链接一个静态库,你可以这样做:
如果您想添加任何这些库所在的目录,请执行以下操作:
现在您添加一个可执行文件,并将其与您的主文件链接:
(我添加了 BlaBla 只是为了表明名称是自定义的).然后你将 LibsModule
与你的可执行模块 MyProgramExecBlaBla
这就行了.
我在你的 CMake 文件中看到的是很多冗余.例如,为什么您有 texture_mapping
,它是包含目录中的一个可执行模块?所以你需要清理它并遵循我解释的简单逻辑.希望它有效.
总而言之,它看起来像这样:
要理解的最重要的事情是模块结构,您可以在其中创建模块并将它们与可执行文件链接在一起.一旦成功,您可以通过更多细节进一步复杂化您的项目.祝你好运!
<小时>注意:请记住,这是使用 CMake 的简单方法.更好的跨平台方式是使用 find_package
,它定位一个包/库,并提供库和包含在 CMake 变量中,以便您可以将您的程序链接到它们.例如,这里是如何为 boost 执行此操作.>
I can't get the additional libraries I am working with to link into my project properly.
I am using CLion, which uses cmake to build it's projects. I am trying to use several libraries in conjunction with OpenGL to texture some objects. I initially built it in Visual Studio, because I couldn't ever figure out how to get cmake to work with Clion. However, now that the code is all working (in Visual Studio, anyways), I want to be able to use CLion for it, because that is my preferred IDE.
I am still new to cmake, and I don't understand what I am doing wrong with my CMakeLists.txt
. Here is what I have:
I tweaked it until it didn't give me any more errors in CLion, but the header files are still not recognized in my code.
Here is the structure of my project:
So, I put all of the libraries I needed, but it doesn't appear to be recognizing them in the code. Clion recognizes them in the project (they aren't appearing red with errors), but when it is built (when I attempt to run it in CLion), I get these errors:
Basically, errors with every usage of SDL and glew, but not glm, which is strange.
What am I doing wrong with my CMakeLists.txt
?
My recommendation is to start simple, and then complicate your project further.
Let me try to explain how linking works in CMake. The idea is that you build modules in CMake, and link them together. Let's ignore header files for now, as they can be all included in your source files.
Say you have file1.cpp, file2.cpp, main.cpp. You add them to your project with:
Now you added them to a module called LibsModule
. Keep that in mind.
Say you want to link to pthread
for example that's already in the system. You can combine it with LibsModule
using the command:
And if you want to link a static library to that too, you do this:
And if you want to add a directory where any of these libraries are located, you do this:
Now you add an executable, and you link it with your main file:
(I added BlaBla just to make it clear that the name is custom). And then you link LibsModule
with your executable module MyProgramExecBlaBla
And this will do it.
What I see in your CMake file is a lot of redundancy. For example, why do you have texture_mapping
, which is an executable module in your include directories? So you need to clean this up and follow the simple logic I explained. Hopefully it works.
In summary, it looks like this:
The most important thing to understand is the module structure, where you create modules and link them all together with your executable. Once this works, you can complicate your project further with more details. Good luck!
Note: Keep in mind that this is the simple way to use CMake. The better cross-platform way would be using find_package
, which locates a package/library, and provides the libraries and includes in CMake variables so that you could link your program to them. Here's how to do this for boost, for example.
这篇关于如何正确链接库与 cmake?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!