我正在使用来自Mac的CLion,并且我不太明白如何将外部库添加到我的项目中。那么,如何将外部库添加到C++项目中呢?
我正在使用来自Mac的CLion,并且我不太明白如何将外部库添加到我的项目中。那么,如何将外部库添加到C++项目中呢?
在CMakeLists.txt末尾手动添加以下行,并使用适合您系统的正确路径和正确的ProjectName。此配置适用于Ubuntu 17.04工作站。
include_directories("/usr/include/SDL2")
target_link_libraries(ProjectName "/usr/lib/x86_64-linux-gnu/libSDL.so")
希望这能有所帮助。
您可以使用以下内容进行测试:
#include <iostream>
#include <SDL.h>
using namespace std;
int main() {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
cout << "SDL Init failed" << endl;
return 1;
}
cout << "SDL Init succeeded" << endl;
SDL_Quit();
return 0;
}
CMakeLists.txt
中添加外部库信息。首先,您可以为此外部库定义一个逻辑名称,例如,我们想要链接的共享库已经在系统上安装了一个 .so 文件。
add_library(myLogicalExtLib SHARED IMPORTED)
这里的 IMPORTED 表示该库已经存在,我们不需要在此项目中构建它。set_target_properties(myLogicalExtLib PROPERTIES IMPORTED_LOCATION "/usr/lib/x86_64-linux-gnu/my_logical_ext_lib.so")