0

Assume I use cmake to create a project like this:

${root}/lib/CMakeLists.txt
add_library(FooLib SHARED ${SOURCES})

${root}/main/CMakeLists.txt
add_executable(Main main.cpp)
target_link_libraries(Main FooLib)

Now if i run Main on linux(fedora in my case), it will auto load the FooLib.so. But on windows, Main.exe will complain about cannot find FooLib.dll. Of courese, if i copy FooLib.dll or manually specify a PATH, the Main.exe will also work fine.

My question are, is this because lib&dll workflow on windows?

And is there any way the Main.exe can remeber the path of FooLib library like on Linux?

ZenWu
  • 38
  • 7
  • Possible duplicate of [DLL search on windows](https://stackoverflow.com/questions/2463243/dll-search-on-windows) – Tsyvarev Dec 01 '17 at 07:53

1 Answers1

-1

If your linking your executable with a lib statically or dynamically, the corresponding dll needs to be in the same folder as the exe. This is a standard design of windows application. The main reason is while loading the dll the exe which contains references of exported functions(this is supplied by the lib) calls the implementation of these functions which reside inside the dll.

You can put your dll inside system32 folder which will be usually in C:\Program files or c:\windows folder depending on the OS type 32/64bit etc since these are defined as system environment paths/variables in windows.

Preference will be always given to exe residing path and then the above mentioned path. Please make sure duplicates of the dll are not there in system32 like similar folders since the exe may load functions of those dlls.

  • Thank you, but not helpful. I already know what you said. I'm asking is there any automatic way, don't need to manually add lib search path. Perhaps some cmake functions or msvc features can be helpful. – ZenWu Dec 01 '17 at 06:31
  • You dont need to add the system32 filepath in windows/anywhere else. By default it is added by the system. You can check the system environment variable list. The %SystemRoot%\system32 path can be seen there. But you need to put your dll in this folder. –  Dec 01 '17 at 06:43