22

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?

genpfault
  • 51,148
  • 11
  • 85
  • 139
PazzoTotale
  • 411
  • 2
  • 5
  • 14
  • 1
    While I'm not very familiar with CLion, it's simply an IDE, not a compiler. For such things you'd need a build system. I'd recommend using modern [CMake](https://cmake.org/) (version 3.2+ is reasonable, but use the latest you can). – tambre Jul 09 '17 at 15:54
  • 1
    CLion works directly with cmake-files. You have to edit CMakeLists.txt by hand – vatosarmat Jul 10 '17 at 01:52

2 Answers2

16

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;
}
Kevin Genus
  • 260
  • 2
  • 7
6

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

ameet chaubal
  • 1,440
  • 16
  • 37