1

I am trying to develop a basic C++ application with multifile programming. I use Cmake to help build a Header file's implementation in the .cpp as a shared library (.dll) in a folder to contain it. and the exe resides in the parent folder of the build. How do I setup my program on Windows to have the exe know to search in the folder for the dll to run the application. I see many professional software have the exe in the parent folder and the dlls stored away in organized folders and the exe can find them. How do they accomplish this?

cmake_minimum_required(VERSION 3.26)

project(mathlearning)

#Compiler options for better debugging
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -g")

add_subdirectory(math_lib)
add_subdirectory(print_lib)

add_executable(mathlearning main.cpp)
target_link_libraries(mathlearning math_lib print_lib)

My main cmake file looks like that. I have two more folders, math_lib (static linked) and print_lib (dynamic linked). The dynamic linked can only be found when I place it in the same folder as the exe. But I want it to be organized in its own folder like much of the professional software I use.

My file/folder structure Inside the math_lib folder

Cmake code inside math_lib:

add_library(math_lib STATIC math_lib.cpp)

Inside the print_lib folder

Cmake code inside the print_lib:

add_library(print_lib SHARED print_lib.cpp)
target_link_libraries(print_lib math_lib) #This shared library depends on the static library

What am I doing wrong?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
ProximaB
  • 11
  • 1
  • [Can this help?](/questions/18917178/how-to-set-dll-search-path) – Botje Aug 30 '23 at 07:53
  • This is not something you normally do. *"I see many professional software have"* Which ones exactly? If the DLLs are not something they load conditionally at runtime (like plugins), I would expect them to keep them near the exe. – HolyBlackCat Aug 30 '23 at 08:01

1 Answers1

1

I see many professional software have the exe in the parent folder and the dlls stored away in organized folders and the exe can find them. How do they accomplish this?

Basically, there are a few different options:

  • put a DLL's folder path in the OS's %PATH% environment variable (works with static linking and dynamic loading).

  • put a DLL's absolute file path in the HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\KnownDLLs Registry key (works with static linking and dynamic loading).

  • have the EXE load a DLL using its absolute file path directly (works with dynamic loading only).

  • call SetDllDirectory() or AddDllDirectory() before loading a DLL by its file name (works with dynamic loading only).

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770