#Help with CMake linking libraries
67 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
I'm just a little frustrated, because not understanding this core concept of building and compiling C++ code is preventing me from diving head-first into learning C++ past the general syntax-
What library do you wanna link?
And show your cmakelists.txt and the errors you get, if any
okay so- I want to link glew and wxWidgets-
but I like dont understand how to link any libraries in CMake
Okay. What OS and compiler?
I tested on OpenAL though- and successfully compiled it and ran it
But VSCode doesnt know where those files are-
Windows 10, Clang++ Visual Studio Code
yes ive been following it! :>
I really need to write more about cmake 😛
What you do is:
- Install the library in msys2 if possible
- In cmakelists.txt, do
find_package(WhateverLibrary REQUIRED) - In cmakelists.txt, do
target_link_libraries(YourTarget PUBLIC WhateverLibrary::WhateverLibrary)
The exact spelling of WhateverLibrary and WhateverLibrary::WhateverLibrary is sometimes not obvious. I don't remember how to figure it out from scratch for a certain library, usually I just google
Works for both. I think it prefers shared (dynamic) libraries by default if both are available
I don't use cmake too much, so I don't remember how to force it to use static
But I'm not a big fan of static libs either
ah okay-
another question; I put my libraries in two folders in the project file, named /include/ and /lib/ respectively- and I plan to put all of the libraries in those
How would I point CMake to those?
Mmm, so you want to manually download the libraries instead of installing them in msys2?
I would like to learn both ways yeah-
But is it better to just link through a msys2 install of the library?
like is there gonna be a library that i cant download in msys2?
and what would the 'YourTarget' be? :0
Definitely a lot easier to do it in msys2. But yeah, it's good to know how to install them elsewhere too
So the "proper" way is to have a directory for the libraries that will contain include, lib, and some other directories. The directory that contains those is often called a "prefix".
To consume a library from a specific prefix, you do -DCMAKE_PREFIX_PATH=path/to/prefix. You can specify multiple paths too (the separator is either ; or :, could be OS-dependent, try both)
So you can do one prefix per library if you prefer, or one fat one
And if you use CMAKE_PREFIX_PATH like this, find_package will magically work
would cmake know where the .lib and .h files are just by pointing to the prefix folder?
like, if they were contained in /lib/ and /include/
In the prefix there would also be some .cmake files or whatever that tell it how to use specific libraries
You don't create a prefix manually (by creating directories include, lib or whatever). Instead, when compiling the library, you specify -DCMAKE_INSTALL_PREFIX=path/to/prefix, and it'll create the prefix at that path and install (copy) the library to it
And that would be the name you passed to your add_executable (or add_library if you're making a library)
ohhhh when you compile the library got it
Yeah
oh okay!
okay so OpenAL is installed in msys2-
but it looks like its not able to find OpenAL
OpenAL is installed though
Cmake has to be installed in msys2
oh okay lmao
Yeah. There's probably a way to use the one from the installer too, but msys2 cmake searches libraries installed in msys2 by default, which is nice
so, do i have to configure that differently in VSCode? or when I install it through msys2 it will just point to the msys2 directories?
You just need to use msys2 cmake instead of your normal cmake. But I don't use the VSC cmake extension, so I'm not sure how to tell it to use a different cmake
There's probably a setting somewhere
also- theres alot of cmakes when i do pacman -Ss cmake
How do I know which one to use? Should i go with mingw-w64-clang-x86_64-cmake or the one just called "cmake"
sorry for all the dumb questions- its all pretty confusing to me, im not used to having to manage libraries like this
Yes, mingw-w64-clang-x86_64-cmake is the right one. In general, you wanna use mingw-w64-clang-x86_64-... when they are available
MSYS2 has multiple copies of everything: compilers, libraries, build systems, etc. They are differentiated by those prefixes in the package names, such as this mingw-w64-clang-x86_64-, or no prefix at all in case of just cmake
(I'm using "prefix" in a different meaning here ofc 😛 )
This stuff is explained here: https://stackoverflow.com/q/76552264/2752075
I don't mind the questions at all
okay hmmm- its still not finding OpenAL
Ah
target_link_libraries goes after add_executable
because add_executable is what creates the OpenAL_link_test target
oh! that worked!
ahhh that makes sense yeah
now when im adding multiple libraries- do i add a new line find_package([package_name] REQUIRED) for each new library?
Yeah
then for the target_link_libraries how would I separate that out? or would I need to do multiple target_link_libraries calls
target_link_libraries(blah PUBLIC A::A B::B C::C)
You can separate it into multiple calls, I don't think there's a difference
ah okay! but i cant dot the same for find_package, it has to be separate?