Home > Blockchain >  How to include external library in C from GitHub using CLion and CMake on Windows?
How to include external library in C from GitHub using CLion and CMake on Windows?

Time:11-26

I have an assignment to create a C program, and one of the libraries we are encouraged to use is GLM, found on this GitHub link here: CLion External Libraries

  • Once I get everything into place, how should my CMakeLists.txt file look to be able to link these libraries correctly?
  • So as you can see, being so new to this I can't really identify yet what the overarching process looks like. Here's what my first attempt looked like: (here, I had downloaded the library as a .zip, uncompressed it and copied the whole thing into a lib folder) External Library Attempt

    It seems CMake was able to find GLM, since it printed a version number. But it still won't find the library when writing the "#include" line, so I must be missing a step somewhere. Any and all help is greatly appreciated. If you need any more information from me, just ask!

    CodePudding user response:

    With the help of a friend, I managed to solve it! For posterity, here's the setup that worked for me.

    I downloaded the library from GitHub using the releases page on the right sidebar: GitHub Library Releases

    I extracted the downloaded .zip, and placed the entire resulting folder in my project structure. For that, I made a "lib" folder (though I suspect folder names or structure isn't as important as I first thought). This is my working project structure:
    Working Project Structure

    As you can see, I also no longer have a separate "src" folder, though I'm sure I could add one if need be. The important part here is that each folder level has its own CMakeLists.txt. In short, the CMakeLists in the project root links the "lib" folder, and the CMakeLists in the lib folder links the "glm" folder. And the one in the glm folder was in the library I downloaded, and it already handles all the linking for subsequent folders and files. Here's what the CMakeLists in the project root looks like:
    CMakeLists in Project Root

    Line 7 links the lib folder. Line 8 creates an executable from the source files I've created (main.cpp, set on line 5). And line 9 links the glm library and my executable. Here's what the CMakeLists in the lib folder looks like:
    CMakeLists in Lib Folder

    Not much in there but linking the glm subdirectory, which then already has a library named glm that I can use in the root CMakeLists.

    With all that set up, I can now successfully use the "#include" command in my main.cpp, and it finds the library:
    Successful Library Include

    Hopefully any other libraries I add can follow the same process. If anyone else runs into the same roadblock, I hope this helps clarify things.

    • Related