c++windowscmakevcpkgcmakelists-options

CMake build linking failure (mysql-connector-cpp)


I am trying to build my application using CMake on Windows. I use 3 3rd party lib: Nlohmann Json and Curl which I linked via Vcpkg and mysql-connector-cpp for which I used the msi installer.

This is my CMakeLists.txt

cmake_minimum_required(VERSION 3.15)
project(SpeedyGo)

set(CMAKE_PREFIX_PATH
    "C:/vcpkg/installed/x86-windows"
    "C:/vcpkg/packages/nlohmann-json_x86-windows/share/nlohmann_json"
    "C:/vcpkg/packages/curl_x86-windows/share/curl"
    "C:/Program Files/MySQL/MySQL Connector C++ 8.1"
)

find_package(CURL CONFIG REQUIRED)
find_package(nlohmann_json CONFIG REQUIRED)

add_executable(main main.cpp)

target_include_directories(main PRIVATE
    "C:/Program Files/MySQL/MySQL Connector C++ 8.1/include"
    ${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include
)

target_link_libraries(main PRIVATE CURL::libcurl)
target_link_libraries(main PRIVATE nlohmann_json::nlohmann_json)
target_link_libraries(main PRIVATE mysqlcppconn8)
target_link_libraries(main PRIVATE
    -L"C:/vcpkg/installed/x86-windows/bin"
    -L"C:/Program Files/MySQL/MySQL Connector C++ 8.1/lib64/vs14"
)

This is my vcpkg.json where I setup the lib I needed to install:

cmake_minimum_required(VERSION 3.15)
project(SpeedyGo)

set(CMAKE_PREFIX_PATH
    "C:/vcpkg/installed/x86-windows"
    "C:/vcpkg/packages/nlohmann-json_x86-windows/share/nlohmann_json"
    "C:/vcpkg/packages/curl_x86-windows/share/curl"
    "C:/Program Files/MySQL/MySQL Connector C++ 8.1"
)

find_package(CURL CONFIG REQUIRED)
find_package(nlohmann_json CONFIG REQUIRED)

add_executable(main main.cpp)

target_include_directories(main PRIVATE
    "C:/Program Files/MySQL/MySQL Connector C++ 8.1/include"
    ${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include
)

target_link_libraries(main PRIVATE CURL::libcurl)
target_link_libraries(main PRIVATE nlohmann_json::nlohmann_json)
target_link_libraries(main PRIVATE mysqlcppconn8)
target_link_libraries(main PRIVATE
    -L"C:/vcpkg/installed/x86-windows/bin"
    -L"C:/Program Files/MySQL/MySQL Connector C++ 8.1/lib64/vs14"
)

The .vscode config file (c_cpp_properties.json):

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "${vcpkgRoot}/x64-windows/include",
                "${vcpkgRoot}/x86-windows/include",
                "C:/vcpkg/installed/x86-windows/include",
                "C:/vcpkg/packages/nlohmann-json_x86-windows/include",
                "C:/vcpkg/packages/curl_x86-windows/include",
                "C:/Program Files/MySQL/MySQL Connector C++ 8.1/include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.22621.0",
            "compilerPath": "C:/Mingw/bin/g++.exe",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "${default}",
            "configurationProvider": "ms-vscode.cmake-tools"
        }
    ],
    "version": 4
}

This is the settings.json file:

{
    "cmake.configureArgs": [
        "-DCMAKE_TOOLCHAIN_FILE=C:\\vcpkg\\scripts\\buildsystems\\vcpkg.cmake"
    ],
    "files.associations": {
        "*.json": "jsonc",
        "iomanip": "cpp",
        "new": "cpp",
        "array": "cpp",
        "atomic": "cpp",
        "*.tcc": "cpp",
        "bitset": "cpp",
        "cctype": "cpp",
        "chrono": "cpp",
        "clocale": "cpp",
        "cmath": "cpp",
        "codecvt": "cpp",
        "condition_variable": "cpp",
        "cstdarg": "cpp",
        "cstddef": "cpp",
        "cstdint": "cpp",
        "cstdio": "cpp",
        "cstdlib": "cpp",
        "cstring": "cpp",
        "ctime": "cpp",
        "cwchar": "cpp",
        "cwctype": "cpp",
        "deque": "cpp",
        "forward_list": "cpp",
        "list": "cpp",
        "unordered_map": "cpp",
        "unordered_set": "cpp",
        "vector": "cpp",
        "exception": "cpp",
        "algorithm": "cpp",
        "any": "cpp",
        "functional": "cpp",
        "iterator": "cpp",
        "map": "cpp",
        "memory": "cpp",
        "memory_resource": "cpp",
        "numeric": "cpp",
        "optional": "cpp",
        "random": "cpp",
        "ratio": "cpp",
        "set": "cpp",
        "string": "cpp",
        "string_view": "cpp",
        "system_error": "cpp",
        "tuple": "cpp",
        "type_traits": "cpp",
        "utility": "cpp",
        "fstream": "cpp",
        "initializer_list": "cpp",
        "iosfwd": "cpp",
        "iostream": "cpp",
        "istream": "cpp",
        "limits": "cpp",
        "mutex": "cpp",
        "ostream": "cpp",
        "sstream": "cpp",
        "stdexcept": "cpp",
        "streambuf": "cpp",
        "thread": "cpp",
        "cinttypes": "cpp",
        "typeinfo": "cpp",
        "valarray": "cpp"
    },
    "cmake.sourceDirectory": "C:/Users/Sorin/Documents/GitHub/SpeedyGo/src",
    "cmake.configureSettings": {
        "VCPKG_TARGET_TRIPLET": "x86-windows"
    }
}

The CMake config process works just fine:

[main] Configuring project: SpeedyGo 
[driver] Removing c:/Users/Sorin/Documents/GitHub/SpeedyGo/build/CMakeCache.txt
[driver] Removing c:\Users\Sorin\Documents\GitHub\SpeedyGo\build\CMakeFiles
[proc] Executing command: C:\cmake\bin\cmake.EXE --no-warn-unused-cli -DCMAKE_TOOLCHAIN_FILE=C:\vcpkg\scripts\buildsystems\vcpkg.cmake -DCMAKE_BUILD_TYPE:STRING=Debug -DVCPKG_TARGET_TRIPLET:STRING=x86-windows -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_C_COMPILER:FILEPATH=C:\MinGW\bin\gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=C:\MinGW\bin\g++.exe -SC:/Users/Sorin/Documents/GitHub/SpeedyGo/src -Bc:/Users/Sorin/Documents/GitHub/SpeedyGo/build -G "MinGW Makefiles"
[cmake] Not searching for unused variables given on the command line.
[cmake] -- Running vcpkg install
[cmake] Detecting compiler hash for triplet x64-windows...
[cmake] Detecting compiler hash for triplet x86-windows...
[cmake] All requested packages are currently installed.
[cmake] Total install time: 300 ns
[cmake] curl provides CMake targets:
[cmake] 
[cmake]     # this is heuristically generated, and may not be correct
[cmake]     find_package(CURL CONFIG REQUIRED)
[cmake]     target_link_libraries(main PRIVATE CURL::libcurl)
[cmake] 
[cmake] The package nlohmann-json provides CMake targets:
[cmake] 
[cmake]     find_package(nlohmann_json CONFIG REQUIRED)
[cmake]     target_link_libraries(main PRIVATE nlohmann_json::nlohmann_json)
[cmake] 
[cmake] The package nlohmann-json can be configured to not provide implicit conversions via a custom triplet file:
[cmake] 
[cmake]     set(nlohmann-json_IMPLICIT_CONVERSIONS OFF)
[cmake] 
[cmake] For more information, see the docs here:
[cmake]     
[cmake]     https://json.nlohmann.me/api/macros/json_use_implicit_conversions/
[cmake] 
[cmake] -- Running vcpkg install - done
[cmake] -- The C compiler identification is GNU 6.3.0
[cmake] -- The CXX compiler identification is GNU 6.3.0
[cmake] -- Detecting C compiler ABI info
[cmake] -- Detecting C compiler ABI info - done
[cmake] -- Check for working C compiler: C:/MinGW/bin/gcc.exe - skipped
[cmake] -- Detecting C compile features
[cmake] -- Detecting C compile features - done
[cmake] -- Detecting CXX compiler ABI info
[cmake] -- Detecting CXX compiler ABI info - done
[cmake] -- Check for working CXX compiler: C:/MinGW/bin/g++.exe - skipped
[cmake] -- Detecting CXX compile features
[cmake] -- Detecting CXX compile features - done
[cmake] -- Found ZLIB: optimized;C:/Users/Sorin/Documents/GitHub/SpeedyGo/build/vcpkg_installed/x86-windows/lib/zlib.lib;debug;C:/Users/Sorin/Documents/GitHub/SpeedyGo/build/vcpkg_installed/x86-windows/debug/lib/zlibd.lib (found suitable version "1.2.13", minimum required is "1")  
[cmake] -- Found nlohmann_json: C:/vcpkg/installed/x86-windows/share/nlohmann_json/nlohmann_jsonConfig.cmake (found version "3.11.2") 
[cmake] -- Configuring done (12.6s)
[cmake] -- Generating done (0.0s)
[cmake] -- Build files have been written to: C:/Users/Sorin/Documents/GitHub/SpeedyGo/build

** !!!!!!!!!!!!!!-The BUILD ERROR i am getting-!!!!!!!!!!!!!!:**

[main] Building folder: SpeedyGo 
[build] Starting build
[proc] Executing command: C:\cmake\bin\cmake.EXE --build c:/Users/Sorin/Documents/GitHub/SpeedyGo/build --config Debug --target all -j 14 --
[build] [ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.obj
[build] [100%] Linking CXX executable main.exe
[build] CMakeFiles\main.dir/objects.a(main.cpp.obj): In function `check_lib':
[build] C:/PROGRA~1/MySQL/MYSQLC~1.1/include/jdbc/cppconn/driver.h:82: undefined reference to `check(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
[build] C:/PROGRA~1/MySQL/MYSQLC~1.1/include/jdbc/cppconn/driver.h:83: undefined reference to `check(std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > const&)'
[build] CMakeFiles\main.dir/objects.a(main.cpp.obj): In function `get_driver_instance_by_name':
[build] C:/PROGRA~1/MySQL/MYSQLC~1.1/include/jdbc/cppconn/driver.h:95: undefined reference to `_get_driver_instance_by_name'
[build] CMakeFiles\main.dir/objects.a(main.cpp.obj): In function `get_driver_instance_by_name':
[build] C:/PROGRA~1/MySQL/MYSQLC~1.1/include/jdbc/mysql_driver.h:116: undefined reference to `sql::mysql::_get_driver_instance_by_name(char const*)'
[build] collect2.exe: error: ld returned 1 exit status
[build] mingw32-make.exe[2]: *** [main.exe] Error 1
[build] CMakeFiles\main.dir\build.make:99: recipe for target 'main.exe' failed
[build] mingw32-make.exe[1]: *** [CMakeFiles/main.dir/all] Error 2
[build] CMakeFiles\Makefile2:81: recipe for target 'CMakeFiles/main.dir/all' failed
[build] mingw32-make.exe: *** [all] Error 2
[build] Makefile:89: recipe for target 'all' failed
[proc] The command: C:\cmake\bin\cmake.EXE --build c:/Users/Sorin/Documents/GitHub/SpeedyGo/build --config Debug --target all -j 14 -- exited with code: 2
[driver] Build completed: 00:00:06.413
[build] Build finished with exit code 2

I tried including varius include folders and also trying diffrent things in the CMakeLists.txt. I don't really know how to solve this. Also the problem seems to only be for mysql-connector-cpp lib.

My github prj. if you want to check out my code: https://github.com/sorin373/SpeedyGo/tree/main. I am open to any suggestions!

PS: The app was firstly built on LINUX, Ubunutu where it works perfectly and I later decided to make it Windows comaptible. (I am using Windows 10 and all software is up to date(double checked))

Thanks for the help :D


Solution

    1. remove all your nonsense:
      setting CMAKE_PREFIX_PATH using absolute PATHs or CMake variables reflecting an absolute path!
      None of the above are needed. You only require targets.

    2. Wrong triplet:

      [cmake] Detecting compiler hash for triplet x64-windows...
      [cmake] Detecting compiler hash for triplet x86-windows...
      are not meant for use with MinGW and not link compatible.
      You probably want x86-mingw-dynamic set via VCPKG_TARGET_TRIPLET and VCPKG_HOST_TRIPLET

    3. MYSQL Connector (mysql-connector-cpp) is also available through vcpkg you probably want to use it