#Help with CMake linking libraries

67 messages · Page 1 of 1 (latest)

soft basalt
#

Hey all, so I'm finding it really difficult to understand linking libraries-
can anyone help guide me through linking static and dynamic libraries using CMake and VSCode?

faint ginkgoBOT
#

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.

soft basalt
#

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-

gentle spoke
#

What library do you wanna link?

#

And show your cmakelists.txt and the errors you get, if any

soft basalt
#

okay so- I want to link glew and wxWidgets-
but I like dont understand how to link any libraries in CMake

gentle spoke
#

Okay. What OS and compiler?

soft basalt
#

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

gentle spoke
#

Oh cool, clang! Did you install it from msys2?

#

Followed my tutorial by any chance?

soft basalt
#

yes ive been following it! :>

gentle spoke
#

I really need to write more about cmake 😛

#

What you do is:

  1. Install the library in msys2 if possible
  2. In cmakelists.txt, do find_package(WhateverLibrary REQUIRED)
  3. 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

soft basalt
#

ohhh- and is that for dynamic libraries?

#

or both?

gentle spoke
#

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

soft basalt
#

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?

gentle spoke
#

Mmm, so you want to manually download the libraries instead of installing them in msys2?

soft basalt
#

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?

soft basalt
gentle spoke
#

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

soft basalt
#

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/

gentle spoke
#

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

gentle spoke
soft basalt
#

ohhhh when you compile the library got it

gentle spoke
#

Yeah

gentle spoke
#

Here's how you compile libraries with cmake

soft basalt
#

OpenAL is installed though

gentle spoke
#

Cmake has to be installed in msys2

soft basalt
#

oh okay lmao

gentle spoke
#

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

soft basalt
#

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?

gentle spoke
#

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

soft basalt
#

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

gentle spoke
#

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

#

I don't mind the questions at all

soft basalt
#

okay hmmm- its still not finding OpenAL

gentle spoke
#

Ah

#

target_link_libraries goes after add_executable

#

because add_executable is what creates the OpenAL_link_test target

soft basalt
#

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?

gentle spoke
#

Yeah

soft basalt
#

then for the target_link_libraries how would I separate that out? or would I need to do multiple target_link_libraries calls

gentle spoke
#

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

soft basalt
#

ah okay! but i cant dot the same for find_package, it has to be separate?

gentle spoke
#

I think so

#

Because it can take a bunch of other arguments (the specific library version, etc)

soft basalt
#

ah got it okay! thanks so much for your help i really appreciate it!
first major hurdle tackled!

#

!solved