简体   繁体   中英

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

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: https://github.com/g-truc/glm . I've been trying to figure out how to include this library in my program but I can't make heads or tails of the process. I am new to C++ and CMake, and everything I find when looking it up is either tailored to other OSes or platforms (I'm using Windows 10), or I don't yet understand it. I'd appreciate some help in clarifying the steps I need to take.

Right now, all I have is a simple Hello World main file, and a CMakeLists.txt file auto-generated by CLion. My goal is to be able to write something like "#include <mat4x4.hpp>", and access the methods in the library. Here are some questions I've been running into:

  • Do I need to download this library from GitHub manually in order to use it? (I'm assuming yes, since there doesn't seem to be a package manager)
  • If so, do I need to copy all or parts of it into my program structure? Do I need to separately compile it and add the compiled file into my program structure?
  • When adding external libraries, do I need to sort things into "lib", "include", and "src" folders? Do I need to mark these directories as library, source, or otherwise? CLion already has a little "External Libraries" directory. Do I place things there? (The subfolders in the image were added by CLion)
    CLion 外部库
  • 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) 外部库尝试

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!

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 库发布

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:
工作项目结构

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

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:
Lib 文件夹中的 CMakeLists

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:
成功的图书馆包括

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM