Add external c++ libraries to a CLion project

2019-06-23 16:01发布

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?

2条回答
The star\"
2楼-- · 2019-06-23 16:41

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")

Hope this helps.

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;
}
查看更多
Anthone
3楼-- · 2019-06-23 16:54

in CMakeLists.txt, add external library information. first, you can define a logical name for the external library, say for e.g. we want to link a shared library which has .so file somewhere already installed on the system,

add_library(myLogicalExtLib SHARED IMPORTED)

IMPORTED means that the library already exists and we don't need to build it here in this project.

then, we can supply the location information about this logical library as follows,

set_target_properties(myLogicalExtLib PROPERTIES IMPORTED_LOCATION "/usr/lib/x86_64-linux-gnu/my_logical_ext_lib.so")

查看更多
登录 后发表回答