将外部C ++库添加到CLion项目

将外部C ++库添加到CLion项目

问题描述:

我正在使用Mac上的CLion,但在了解如何向我的项目中添加外部库时遇到了问题.那么,如何将外部库添加到C ++项目中?

I am using CLion from Mac, and i'm having problems to understand how can i add external libraries to my project. So, how can i add external libraries to a c++ project?

手动编辑CMakeLists.txt,在末尾添加以下几行,以及适用于您系统的正确路径和正确的ProjectName.此配置适用于Ubuntu 17.04工作站.

Manually edit CMakeLists.txt adding the following lines at the end with the proper paths for your system and proper ProjectName. This config is for an Ubuntu 17.04 workstation.

include_directories("/usr/include/SDL2")
target_link_libraries(ProjectName "/usr/lib/x86_64-linux-gnu/libSDL.so")

希望这会有所帮助.

您可以使用以下命令对其进行测试:

You can test it with the following:

#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;
}