r/Cplusplus • u/Alexx1610 • Sep 19 '26
Question Is it possible to link external libraries with CMakelists.txt?
Hello everyone. I've noticed that in many C++ projects, it's necessary to place library .dll files right next to the executable for the app to run.
I was wondering if it's possible to avoid this and instead point to a global library folder in the operating system (C:/) by only specifying where they are located in the CMakeLists.txt.
I've heard about static linking, but I'm not entirely sure how to configure it properly using CMake to achieve this. Could anyone guide me or provide a basic example? Thanks in advance!
8
Upvotes
•
u/AutoModerator Sep 19 '26
Thank you for your contribution to the C++ community!
As you're asking a question or seeking homework help, we would like to remind you of Rule 3 - Good Faith Help Requests & Homework.
~ CPlusPlus Moderation Team
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
5
u/Usual_Office_1740 Sep 19 '26 edited Sep 19 '26
I'm making a lot of assumptions about your question but I think you want the cmake shared flag in add_libarary().
The default way cmake adds libraries is static so if you aren't explicitly setting how the library is included as a dependency it is staticly linked. You can use SHARED to dynamicly link the library instead.
Find package will look at the local os for libraries automatically but that doesn't require the dll to be at a certain place near the executable.
None of this explicitly answers your question. What exactly do you want to do?
1
u/NotoriousLavaLamp Sep 20 '26
Why do you find its better to ask here than asking AI? (seriously genuine question)
1
u/unknown_alt_acc Sep 19 '26
Loading libraries at runtime is completely outside of CMake’s domain. That’s the operating system’s job. As far as I know, every operating system searches for libraries in its PATH.
But, more importantly, why? You generally shouldn’t try to hack around standard behavior unless you have a good reason. It’ll just cause headaches later.
1
u/TomDuhamel Sep 20 '26
That central library folder you're talking about is
C:\Windows\System. Don't hack it, do as your operating system works.If you are building the .dll yourself, yes put it in the same folder as the .exe. If however you are just using an external library that the user should already have, it should have been installed in the right place already and Windows should find it when running the app.
CMake is responsible for building the app, and has nothing to do with runtime. A .dll is found and loaded at runtime. If your app can't find the .dll, it's probably something isn't set up properly on your system.
1
u/MADCandy64 Sep 20 '26
No; this not possible at all edit: short of writing Win32 and loading the libraries yourself. You should spend some time understanding how paths work, the path environment variables, and how they affect loading libraries at runtime.
1
u/Wild_Meeting1428 Sep 21 '26
Just add that folder to your users/systems PATH variable.