0

So I have my root CMakeLists.txt here:

cmake_minimum_required(VERSION 3.16)
project(Vibranium_Core)

set(CMAKE_CXX_STANDARD 14)

find_package(Boost 1.72.0)

if(NOT Boost_FOUND)
    message(FATAL_ERROR "Could not find boost!")
endif()
include_directories(${Boost_INCLUDE_DIR})

if("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
    message(STATUS "Target is 64 bits")
    if (WIN32)
        set(WINXXBITS Win64)
    endif(WIN32)
else("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
    message(STATUS "Target is 32 bits")
    if (WIN32)
        set(WINXXBITS Win32)
    endif(WIN32)
endif("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
find_package(MySQL REQUIRED)

include_directories(${MYSQL_INCLUDE_DIR})

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/Source/Common)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/Source/Core/WorldServer)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/Source/Core/AuthServer)

add_subdirectory(Tests)

set_target_properties(VibraniumCoreTests PROPERTIES EXCLUDE_FROM_ALL TRUE)
set_target_properties(gtest PROPERTIES EXCLUDE_FROM_ALL TRUE)
set_target_properties(gmock PROPERTIES EXCLUDE_FROM_ALL TRUE)
set_target_properties(gtest_main PROPERTIES EXCLUDE_FROM_ALL TRUE)
set_target_properties(gmock_main PROPERTIES EXCLUDE_FROM_ALL TRUE)

set_target_properties(
        Common WorldServer AuthServer
        PROPERTIES
        ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/lib"
        LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/lib"
        RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
)

What I want to achieve is both AuthServer & WorldServer targets to use shared library called by me Common. So far so good. I come to a stage where I want to provide mysql functionalities to my Common library. In order to do that I have to include it in Common and link the MySQL Connector.

So here is my Common CMakeLists.txt file contents:

add_library(
    Common
        SHARED
        Config.cpp
        Config.h
        Logger.cpp
        Logger.h
        Database/DatabaseLoader.cpp
        Database/DatabaseLoader.h
        Database/MySQLConnection.h
        Database/MySQLConnection.cpp
        Database/MySQLTable.h
        Database/MySQLTable.cpp
    )
if(WIN32)

    target_include_directories(Common PUBLIC
            ${FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR}/include
            )

    # Link the MySQL library to your executable.
    target_link_libraries(Common PUBLIC
            ${FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR}/lib64/vs14/mysqlcppconn8.lib
            )
else()
target_link_libraries(Common LINK_PUBLIC ${MYSQL_LIBRARY} mysqlcppconn)
endif()
target_include_directories(Common PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

So with this my AuthServer target builds successfully. However when I try to start it it says:

The code execution cannot proceed because mysqlcppconn8-2-vs14.dll was not found. Reinstalling the program may fix this problem.

In order to make the program run I have to manually copy from here C:\Program Files\MySQL\Connector C++ 8.0\lib64\mysqlcppconn8-2-vs14.dll into the /bin folder of AuthServer.

What I want to achive is to force AuthServer and WorldServer to look for mysqlcppconn8-2-vs14.dll inside build_dir/lib folder instead of the root build directory.

How can I achieve that ?

Venelin
  • 2,905
  • 7
  • 53
  • 117
  • What is wrong in setting variable `CMAKE_RUNTIME_OUTPUT_DIRECTORY`, like in [that answer](https://stackoverflow.com/a/34445220/3440745) to the (duplicate?) question? – Tsyvarev Aug 31 '20 at 12:55
  • Where should I set it? In the root `CMakeLists.txt` or ? – Venelin Aug 31 '20 at 12:56
  • You may set it in the root `CMakeLists.txt`, before creating libraries/executables and before `add_subdirectory` calls. – Tsyvarev Aug 31 '20 at 12:59
  • 1
    Is the referenced answer is somehow unclear? Otherwise, why should one copy it here? And why have you deleted your [previous question](https://stackoverflow.com/questions/63670650/cmake-how-to-import-dependency-library-correctly-into-another-one) about the same problem? – Tsyvarev Aug 31 '20 at 13:04
  • What you suggest does not work.That's why I would like to see answer where I can see where you suggest the change to be placed. :) – Venelin Aug 31 '20 at 13:07
  • 1
    Well, that approach won't help in finding `mysqlcppconn8-2-vs14.dll` at runtime. But as already has been noted in the comments to your previous question, you need either to copy that library near your libraries, or to add path to that library into the `PATH` variable. Windows doesn't allow to specify RPATH in the compiled library, like in Linux. – Tsyvarev Aug 31 '20 at 13:12
  • I will copy `mysqlcppconn8-2-vs14.dll` but in this question i am asking how to make the targets look for it in `BUILD_DIR/lib` folder instead of root `BUILD_DIR`. Can you please help out on that? Otherwise I know how to copy `mysqlcppconn8-2-vs14.dll`. – Venelin Aug 31 '20 at 13:14
  • "how to make the targets look for it in `BUILD_DIR/lib` folder instead of root `BUILD_DIR`" - I don't understand that. What do you mean by "target look"? – Tsyvarev Aug 31 '20 at 13:25
  • When I run `AuthServer.exe` it runs if I have next to it `mysqlcppconn8-2-vs14.dll`. What I want is when I run `AuthServer.exe` to run fine when `mysqlcppconn8-2-vs14.dll` is located NOT next to it but in `/lib` folder. – Venelin Aug 31 '20 at 13:28
  • This may be possible with a manifest file. I am not sure. Nothing else you can do at build time can influence this. – drescherjm Aug 31 '20 at 13:37
  • Related to the manifest file: [https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/3e82b658-e070-4b88-aaf2-0649ddb222d2/how-to-write-manifest-file-to-pick-up-dlls-via-dll-redirection?forum=vcgeneral](https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/3e82b658-e070-4b88-aaf2-0649ddb222d2/how-to-write-manifest-file-to-pick-up-dlls-via-dll-redirection?forum=vcgeneral) – drescherjm Aug 31 '20 at 13:47
  • [That answer](https://stackoverflow.com/a/25407687/3440745) describes the search algorithm of `.dll` at runtime. As you can see, you cannot force the system to search arbitrary paths outside of `PATH` variable. – Tsyvarev Aug 31 '20 at 14:27

0 Answers0