通过CMake添加外部库(例如,Eigen) [英] Add an external library (e.g., Eigen) via CMake

查看:975
本文介绍了通过CMake添加外部库(例如,Eigen)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经处理这个问题几个星期了。我知道我可能只是错过一个小步,我不知道。所以,任何洞察将非常感激。

I've been dealing with this issue for a few weeks now. I know that I'm probably just missing a minor step that I'm not aware of. So, any insight would be highly appreciated.

我试图添加一些外部库,并在我的C + +项目中使用它们。但是似乎没有能够弄清楚如何。我读了所有在互联网上关于添加外部库的帖子,但我相信我错过了一些东西!

I'm trying to add some external libraries and use them in my C++ project. But don't seem to be able to figure out how. I read all the posts on internet about adding an external library but I believe I'm missing something!

我想说,我想添加一个简单的库,如Eigen标题库)。我复制并粘贴了包含项目中子文件夹中所有文件的文件夹。我调用了子文件夹ThirdPartyLibraries。

Let's say, I want to add a simple library like Eigen (a header-only library). I copy and pasted the folder containing all the files in a subfolder in my project. I called the subfolder ThirdPartyLibraries.

INCLUDE_DIRECTORIES("${MAINFOLDER}/ThirdPartyLibraries/eigen")

我还需要做什么吗?

我想添加另一个库像XGBoost(它有头文件和源文件)。它会有什么不同?

Now, let's say I want to add another library like XGBoost (which has both header and source files). How would it be different?

我对XGBoost做了类似的事情。我的代码可以构建没有错误,但只要我不指向任何函数库(基本上只是添加库到我的项目,而不使用它)。当我尝试使用任何函数时,我得到关于未解决的外部符号的错误,这使我认为我在添加库时缺少一些东西。

I did similar things for XGBoost. My code may build with no error but that's as long as I don't refer to any of the functions in the library (basically just adding the library to my project without using it). When I try to use any of the functions, I get errors about "unresolved external symbol"s which makes me think I'm missing something while adding the libraries.

没有添加任何库到我的C ++代码。

I've not added any libraries to my C++ code. So, if you can keep the explanations simple, that'd be great!

推荐答案

所以,我解决了这个问题,认为我为任何可能遇到此问题的人分享解决方案。

So, I resolved this problem and thought I share the solution for anyone else who might come across this problem.

添加Eigen库非常简单,因为它只是头文件。您只需将所有头文件复制并粘贴到项目目录中的某个位置,并使用include目录。你应该很好。

The adding Eigen library is pretty straightforward as it's only header files. You just copy and paste all the header files somewhere in your project's directory and use include directories. You should be fine after.

关于XGBoost链接,他们只是添加了该项目的cmake文件。所以,你需要做的是在Visual Studio中构建该项目。构建后,您将获得.lib,.dll和.exe文件。你应该只需要.lib文件。将它们复制并粘贴到项目目录中的某个位置,并将其链接到主项目,如下所示:

Regarding the XGBoost linking, they just added the cmake file for that project. So, what you need to do is to build that project in Visual Studio. After building, you get .lib, .dll, and .exe files. You should only need .lib file. Copy and paste them somewhere in your project's directory and link it to the main project like this:

TARGET_LINK_LIBRARIES(ProjectName libxgboost)

然后,您必须处理并从xgboost目录将include目录(包含头文件)粘贴到某处在您的项目目录中。除了libxgboost,你还必须包括rabit和xgboost库。所以,只需复制和粘贴它们,当你复制和粘贴libxgboost.lib并链接他们使用'target_link_directories'。

Then, you have to cope and paste the include directory (containing the header files) from the xgboost directory to somewhere in your project directory. Besides libxgboost, you have to include rabit and xgboost libraries too. So, just copy and paste them when you're copy and pasting libxgboost.lib and link them using 'target_link_directories'.

当你使用任何功能,

希望这可以帮助那些难以使用它的人。

Hope this helps others who have a hard time getting it to work.

这篇关于通过CMake添加外部库(例如,Eigen)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆