CMake构建链接失败(mysql-connector-cpp)

ct3nt3jp  于 2023-10-20  发布在  Mysql
关注(0)|答案(1)|浏览(183)

我正在尝试在Windows上使用CMake构建我的应用程序。我使用3个第三方库:Nlohmann Json和Curl,我通过Vcpkg和mysql-connector-cpp链接,我使用msi安装程序。

这是我的CMakeLists.txt

  1. cmake_minimum_required(VERSION 3.15)
  2. project(SpeedyGo)
  3. set(CMAKE_PREFIX_PATH
  4. "C:/vcpkg/installed/x86-windows"
  5. "C:/vcpkg/packages/nlohmann-json_x86-windows/share/nlohmann_json"
  6. "C:/vcpkg/packages/curl_x86-windows/share/curl"
  7. "C:/Program Files/MySQL/MySQL Connector C++ 8.1"
  8. )
  9. find_package(CURL CONFIG REQUIRED)
  10. find_package(nlohmann_json CONFIG REQUIRED)
  11. add_executable(main main.cpp)
  12. target_include_directories(main PRIVATE
  13. "C:/Program Files/MySQL/MySQL Connector C++ 8.1/include"
  14. ${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include
  15. )
  16. target_link_libraries(main PRIVATE CURL::libcurl)
  17. target_link_libraries(main PRIVATE nlohmann_json::nlohmann_json)
  18. target_link_libraries(main PRIVATE mysqlcppconn8)
  19. target_link_libraries(main PRIVATE
  20. -L"C:/vcpkg/installed/x86-windows/bin"
  21. -L"C:/Program Files/MySQL/MySQL Connector C++ 8.1/lib64/vs14"
  22. )

这是我的vcpkg.json,我在这里设置了需要安装的lib:

  1. cmake_minimum_required(VERSION 3.15)
  2. project(SpeedyGo)
  3. set(CMAKE_PREFIX_PATH
  4. "C:/vcpkg/installed/x86-windows"
  5. "C:/vcpkg/packages/nlohmann-json_x86-windows/share/nlohmann_json"
  6. "C:/vcpkg/packages/curl_x86-windows/share/curl"
  7. "C:/Program Files/MySQL/MySQL Connector C++ 8.1"
  8. )
  9. find_package(CURL CONFIG REQUIRED)
  10. find_package(nlohmann_json CONFIG REQUIRED)
  11. add_executable(main main.cpp)
  12. target_include_directories(main PRIVATE
  13. "C:/Program Files/MySQL/MySQL Connector C++ 8.1/include"
  14. ${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include
  15. )
  16. target_link_libraries(main PRIVATE CURL::libcurl)
  17. target_link_libraries(main PRIVATE nlohmann_json::nlohmann_json)
  18. target_link_libraries(main PRIVATE mysqlcppconn8)
  19. target_link_libraries(main PRIVATE
  20. -L"C:/vcpkg/installed/x86-windows/bin"
  21. -L"C:/Program Files/MySQL/MySQL Connector C++ 8.1/lib64/vs14"
  22. )

.vscode配置文件(c_cpp_properties.json):

  1. {
  2. "configurations": [
  3. {
  4. "name": "Win32",
  5. "includePath": [
  6. "${workspaceFolder}/**",
  7. "${vcpkgRoot}/x64-windows/include",
  8. "${vcpkgRoot}/x86-windows/include",
  9. "C:/vcpkg/installed/x86-windows/include",
  10. "C:/vcpkg/packages/nlohmann-json_x86-windows/include",
  11. "C:/vcpkg/packages/curl_x86-windows/include",
  12. "C:/Program Files/MySQL/MySQL Connector C++ 8.1/include"
  13. ],
  14. "defines": [
  15. "_DEBUG",
  16. "UNICODE",
  17. "_UNICODE"
  18. ],
  19. "windowsSdkVersion": "10.0.22621.0",
  20. "compilerPath": "C:/Mingw/bin/g++.exe",
  21. "cStandard": "c17",
  22. "cppStandard": "c++17",
  23. "intelliSenseMode": "${default}",
  24. "configurationProvider": "ms-vscode.cmake-tools"
  25. }
  26. ],
  27. "version": 4
  28. }

settings.json文件:

  1. {
  2. "cmake.configureArgs": [
  3. "-DCMAKE_TOOLCHAIN_FILE=C:\\vcpkg\\scripts\\buildsystems\\vcpkg.cmake"
  4. ],
  5. "files.associations": {
  6. "*.json": "jsonc",
  7. "iomanip": "cpp",
  8. "new": "cpp",
  9. "array": "cpp",
  10. "atomic": "cpp",
  11. "*.tcc": "cpp",
  12. "bitset": "cpp",
  13. "cctype": "cpp",
  14. "chrono": "cpp",
  15. "clocale": "cpp",
  16. "cmath": "cpp",
  17. "codecvt": "cpp",
  18. "condition_variable": "cpp",
  19. "cstdarg": "cpp",
  20. "cstddef": "cpp",
  21. "cstdint": "cpp",
  22. "cstdio": "cpp",
  23. "cstdlib": "cpp",
  24. "cstring": "cpp",
  25. "ctime": "cpp",
  26. "cwchar": "cpp",
  27. "cwctype": "cpp",
  28. "deque": "cpp",
  29. "forward_list": "cpp",
  30. "list": "cpp",
  31. "unordered_map": "cpp",
  32. "unordered_set": "cpp",
  33. "vector": "cpp",
  34. "exception": "cpp",
  35. "algorithm": "cpp",
  36. "any": "cpp",
  37. "functional": "cpp",
  38. "iterator": "cpp",
  39. "map": "cpp",
  40. "memory": "cpp",
  41. "memory_resource": "cpp",
  42. "numeric": "cpp",
  43. "optional": "cpp",
  44. "random": "cpp",
  45. "ratio": "cpp",
  46. "set": "cpp",
  47. "string": "cpp",
  48. "string_view": "cpp",
  49. "system_error": "cpp",
  50. "tuple": "cpp",
  51. "type_traits": "cpp",
  52. "utility": "cpp",
  53. "fstream": "cpp",
  54. "initializer_list": "cpp",
  55. "iosfwd": "cpp",
  56. "iostream": "cpp",
  57. "istream": "cpp",
  58. "limits": "cpp",
  59. "mutex": "cpp",
  60. "ostream": "cpp",
  61. "sstream": "cpp",
  62. "stdexcept": "cpp",
  63. "streambuf": "cpp",
  64. "thread": "cpp",
  65. "cinttypes": "cpp",
  66. "typeinfo": "cpp",
  67. "valarray": "cpp"
  68. },
  69. "cmake.sourceDirectory": "C:/Users/Sorin/Documents/GitHub/SpeedyGo/src",
  70. "cmake.configureSettings": {
  71. "VCPKG_TARGET_TRIPLET": "x86-windows"
  72. }
  73. }

CMake配置过程运行良好:

  1. [main] Configuring project: SpeedyGo
  2. [driver] Removing c:/Users/Sorin/Documents/GitHub/SpeedyGo/build/CMakeCache.txt
  3. [driver] Removing c:\Users\Sorin\Documents\GitHub\SpeedyGo\build\CMakeFiles
  4. [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"
  5. [cmake] Not searching for unused variables given on the command line.
  6. [cmake] -- Running vcpkg install
  7. [cmake] Detecting compiler hash for triplet x64-windows...
  8. [cmake] Detecting compiler hash for triplet x86-windows...
  9. [cmake] All requested packages are currently installed.
  10. [cmake] Total install time: 300 ns
  11. [cmake] curl provides CMake targets:
  12. [cmake]
  13. [cmake] # this is heuristically generated, and may not be correct
  14. [cmake] find_package(CURL CONFIG REQUIRED)
  15. [cmake] target_link_libraries(main PRIVATE CURL::libcurl)
  16. [cmake]
  17. [cmake] The package nlohmann-json provides CMake targets:
  18. [cmake]
  19. [cmake] find_package(nlohmann_json CONFIG REQUIRED)
  20. [cmake] target_link_libraries(main PRIVATE nlohmann_json::nlohmann_json)
  21. [cmake]
  22. [cmake] The package nlohmann-json can be configured to not provide implicit conversions via a custom triplet file:
  23. [cmake]
  24. [cmake] set(nlohmann-json_IMPLICIT_CONVERSIONS OFF)
  25. [cmake]
  26. [cmake] For more information, see the docs here:
  27. [cmake]
  28. [cmake] https://json.nlohmann.me/api/macros/json_use_implicit_conversions/
  29. [cmake]
  30. [cmake] -- Running vcpkg install - done
  31. [cmake] -- The C compiler identification is GNU 6.3.0
  32. [cmake] -- The CXX compiler identification is GNU 6.3.0
  33. [cmake] -- Detecting C compiler ABI info
  34. [cmake] -- Detecting C compiler ABI info - done
  35. [cmake] -- Check for working C compiler: C:/MinGW/bin/gcc.exe - skipped
  36. [cmake] -- Detecting C compile features
  37. [cmake] -- Detecting C compile features - done
  38. [cmake] -- Detecting CXX compiler ABI info
  39. [cmake] -- Detecting CXX compiler ABI info - done
  40. [cmake] -- Check for working CXX compiler: C:/MinGW/bin/g++.exe - skipped
  41. [cmake] -- Detecting CXX compile features
  42. [cmake] -- Detecting CXX compile features - done
  43. [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")
  44. [cmake] -- Found nlohmann_json: C:/vcpkg/installed/x86-windows/share/nlohmann_json/nlohmann_jsonConfig.cmake (found version "3.11.2")
  45. [cmake] -- Configuring done (12.6s)
  46. [cmake] -- Generating done (0.0s)
  47. [cmake] -- Build files have been written to: C:/Users/Sorin/Documents/GitHub/SpeedyGo/build
  • !!-我得到的构建错误-!!!!!!!!!!!!!!:****
  1. [main] Building folder: SpeedyGo
  2. [build] Starting build
  3. [proc] Executing command: C:\cmake\bin\cmake.EXE --build c:/Users/Sorin/Documents/GitHub/SpeedyGo/build --config Debug --target all -j 14 --
  4. [build] [ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.obj
  5. [build] [100%] Linking CXX executable main.exe
  6. [build] CMakeFiles\main.dir/objects.a(main.cpp.obj): In function `check_lib':
  7. [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&)'
  8. [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&)'
  9. [build] CMakeFiles\main.dir/objects.a(main.cpp.obj): In function `get_driver_instance_by_name':
  10. [build] C:/PROGRA~1/MySQL/MYSQLC~1.1/include/jdbc/cppconn/driver.h:95: undefined reference to `_get_driver_instance_by_name'
  11. [build] CMakeFiles\main.dir/objects.a(main.cpp.obj): In function `get_driver_instance_by_name':
  12. [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*)'
  13. [build] collect2.exe: error: ld returned 1 exit status
  14. [build] mingw32-make.exe[2]: *** [main.exe] Error 1
  15. [build] CMakeFiles\main.dir\build.make:99: recipe for target 'main.exe' failed
  16. [build] mingw32-make.exe[1]: *** [CMakeFiles/main.dir/all] Error 2
  17. [build] CMakeFiles\Makefile2:81: recipe for target 'CMakeFiles/main.dir/all' failed
  18. [build] mingw32-make.exe: *** [all] Error 2
  19. [build] Makefile:89: recipe for target 'all' failed
  20. [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
  21. [driver] Build completed: 00:00:06.413
  22. [build] Build finished with exit code 2

我尝试包括varius包括文件夹,也尝试在CMakeLists.txt不同的东西。我真的不知道怎么解决这个问题。另外,这个问题似乎只针对mysql-connector-cpplib。
我的github prj。如果你想看看我的代码:https://github.com/sorin373/SpeedyGo/tree/main.我愿意接受任何建议!
PS:该应用程序首先建立在Linux上,Ubuntu在那里它完美地工作,我后来决定让它兼容Windows。(我使用的是Windows 10,所有软件都是最新的(双重检查))
谢谢你的帮助:D

gopyfrb3

gopyfrb31#

1.把你所有的废话都删掉:
使用绝对路径或反映绝对路径的CMake变量设置CMAKE_PREFIX_PATH!
以上都不需要。你只需要目标。
1.错误的三联体:
[cmake]正在检测x64窗口的编译器散列.
[cmake]正在检测x86窗口的编译器散列.
不适用于MinGW,也不兼容链路。
您可能希望通过VCPKG_TARGET_TRIPLETVCPKG_HOST_TRIPLET设置x86-mingw-dynamic

  1. MYSQL连接器(mysql-connector-cpp)也可通过vcpkg获得,您可能希望使用它

相关问题