如何在Windows中使用cmake正确链接ODB库到项目?

问题描述 投票:0回答:1

我已经使用 vcpkg 安装rapidjson和curl,但我发现很难将 PostgreSQL 的 ODB 库链接到我的项目
我已经使用此link
中的说明构建了所需的库 我将 vscode 与 VS2022 工具包一起使用。我在 PATH 和项目目录中有 odb-pgsql-d-2.4-vc12.dll 和 odb-d-2.4-vc12.dll 。
我能够使用 ODB 编译器生成 xxx-odb.hpp 文件,但我无法正确链接该库,看起来像什么? 附加最小的 CMakeLists.txt、main.cpp 和构建输出文件。
我遇到未解决的外部符号错误。

CMakeLists.txt

project(main VERSION 0.1.0 LANGUAGES C CXX)

set(CMAKE_GENERATOR_PLATFORM x64)

add_executable(main main.cpp)

set(VCPKG_INSTALLED_DIR "C:\\vcpkg\\installed\\x64-windows")
set(ODB_COMPILER_PATH "C:\\pathtools\\bin")
set(ODB_INSTALLED_DIR "C:\\external")

find_package(PostgreSQL REQUIRED)
target_link_libraries(main PRIVATE PostgreSQL::PostgreSQL)

find_program(ODB_COMPILER NAMES odb PATHS "${ODB_COMPILER_PATH}" EXTERNAL REQUIRED)
set(ODB_LIB_DIR "${ODB_INSTALLED_DIR}/lib")
set(ODB_INCLUDE_DIR "${ODB_INSTALLED_DIR}/include")
target_include_directories(main PRIVATE ${ODB_INCLUDE_DIR})
link_directories(${ODB_LIB_DIR})
target_link_libraries(main PRIVATE ${ODB_LIB_DIR}/odb-d.lib)
target_link_libraries(main PRIVATE ${ODB_LIB_DIR}/odb-pgsql-d.lib)

add_custom_target(run
    COMMAND ${PROJECT_NAME}
    DEPENDS ${PROJECT_NAME}
    WORKING_DIRECTORY ${CMAKE_PROJECT_DIR}
)

主.cpp

#include <iostream>
#include <memory>
#include <odb/pgsql/database.hxx>
#include <odb/transaction.hxx>

int main()
{
    std::shared_ptr<odb::pgsql::database> db(new odb::pgsql::database("postgres", "password", "maindb", "localhost", 5432));
    return 0;
}

构建日志

[main] Building folder: c:/main/build main
[build] Starting build
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --build c:/main/build --config Debug --target main -j 22 --
[build] MSBuild version 17.11.9+a69bbaaf5 for .NET Framework
[build] 
[build] main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall odb::pgsql::database::database(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,unsigned int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class odb::details::transfer_ptr<class odb::pgsql::connection_factory>)" (__imp_??0database@pgsql@odb@@QAE@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@000I0V?$transfer_ptr@Vconnection_factory@pgsql@odb@@@details@2@@Z) referenced in function _main [C:\main\build\main.vcxproj]
[build] main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __thiscall odb::pgsql::database::~database(void)" (__imp_??1database@pgsql@odb@@UAE@XZ) referenced in function "public: virtual void * __thiscall odb::pgsql::database::`scalar deleting destructor'(unsigned int)" (??_Gdatabase@pgsql@odb@@UAEPAXI@Z) [C:\main\build\main.vcxproj]
[build] main.obj : error LNK2001: unresolved external symbol "public: virtual class odb::pgsql::transaction_impl * __thiscall odb::pgsql::database::begin(void)" (?begin@database@pgsql@odb@@UAEPAVtransaction_impl@23@XZ) [C:\main\build\main.vcxproj]
[build] main.obj : error LNK2001: unresolved external symbol "protected: virtual struct odb::database::schema_version_info const & __thiscall odb::pgsql::database::load_schema_version(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)const " (?load_schema_version@database@pgsql@odb@@MBEABUschema_version_info@13@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) [C:\main\build\main.vcxproj]
[build] main.obj : error LNK2001: unresolved external symbol "protected: virtual class odb::connection * __thiscall odb::pgsql::database::connection_(void)" (?connection_@database@pgsql@odb@@MAEPAVconnection@3@XZ) [C:\main\build\main.vcxproj]
[build] C:\Program Files\PostgreSQL\16\lib\libpq.lib : warning LNK4272: library machine type 'x64' conflicts with target machine type 'x86' [C:\main\build\main.vcxproj]
[build] C:\external\lib\odb-d.lib : warning LNK4272: library machine type 'x64' conflicts with target machine type 'x86' [C:\main\build\main.vcxproj]
[build] C:\external\lib\odb-pgsql-d.lib : warning LNK4272: library machine type 'x64' conflicts with target machine type 'x86' [C:\main\build\main.vcxproj]
[build] C:\main\build\Debug\main.exe : fatal error LNK1120: 5 unresolved externals [C:\main\build\main.vcxproj]
[proc] The command: "C:\Program Files\CMake\bin\cmake.EXE" --build c:/main/build --config Debug --target main -j 22 -- exited with code: 1
[driver] Build completed: 00:00:01.946
[build] Build finished with exit code 1
c++ postgresql cmake odb
1个回答
0
投票

您的构建环境存在几个问题。

odb-pgsql-d-2.4-vc12.dll 和 odb-d-2.4-vc12.dll 及其导入库适用于 Visual Studio 2013,可能与 Visual Studio 2022 不兼容。您可能需要降级 Visual Studio 或升级ODB库。

警告 LNK4272:库计算机类型“x64”与目标计算机类型“x86”冲突

比较明显一点。

main
目标平台是 x86,ODB 库面向 x64。它们不兼容。你还没有展示你如何运行cmake,也许用
-AWin32
,你不应该设置它。

© www.soinside.com 2019 - 2024. All rights reserved.