【问题标题】:Proper CMakeLists file正确的 CMakeLists 文件
【发布时间】:2023-03-13 22:59:01
【问题描述】:

我正在尝试使用 CLionCMakeFilesPostgreSQL 构建一个带有 LIBPQXX Github 的项目。这是我针对这个问题的简单代码,pqxx::connection con(); 函数中的参数是为了帖子而故意更改的。

#include <iostream>
#include <pqxx/pqxx>
#include "project_config.h"


int main()
{
    pqxx::connection conn("dbname = dbname user = user password = password hostaddr = hostaddr port = port");
    std::cout << "Opened database successfully: " << std::endl;
    return 0;
}

当我在 Shell 中执行命令时

g++ -std=c++11 -I /usr/local/include/pqxx/ -L /usr/local/lib/ -I /usr/pgsql-10/include/ -L /usr/pgsql-10/lib/ main.cpp -lpqxx -lpq -o run

之后./run,一切正常。

现在,当我使用那些 CMakeFiles 设置来获得相同的结果以在 CLion

中运行项目时
cmake_minimum_required(VERSION 3.10)
project(project)

# Set flags
set(COMPILE_FLAGS "-std=c++11")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COMPILE_FLAGS}")

# The version number.
set (project_VERSION_MAJOR 1)
set (project_VERSION_MINOR 0)

# Use a header file for some CMake settings in the source code
configure_file (
        "${PROJECT_SOURCE_DIR}/project_config.h.in"
        "${PROJECT_BINARY_DIR}/project_config.h"
)


# Set new own names
set(APP_DOMAIN ${PROJECT_SOURCE_DIR}/dev/hmmenc_client)
set(PQXX /usr/local/include/pqxx)
set(PGSQL_10 /usr/pgsql-10/include)

# Find the pqxx and pq libraries
find_library(PQXX_LIB pqxx "${PQXX}")
find_library(PQ_LIB pq "${PGSQL_10}")


include_directories(${PROJECT_BINARY_DIR})

# Other project path includes
include_directories(${APP_DOMAIN})
include_directories(${PQXX})
include_directories(${PGSQL_10})

find_package(FindPQXX)

set(SOURCE_FILES
#        ${APP_DOMAIN}/db/db.h
#        ${APP_DOMAIN}/db/db.cpp
        ${APP_DOMAIN}/main.cpp
    )

add_executable(project ${SOURCE_FILES})
target_link_libraries(${TARGET_NAME} pqxx)
#link_directories(project ${PQXX})

但是当我尝试运行它时,我得到:

CMakeFiles/project.dir/dev/hmmenc_client/main.cpp.o: In function `pqxx::connection_base::connection_base(pqxx::connectionpolicy&)':
/usr/local/include/pqxx/connection_base.hxx:695: undefined reference to `int pqxx::internal::check_library_version<6, 2>()'
/usr/local/include/pqxx/connection_base.hxx:698: undefined reference to `pqxx::connection_base::clearcaps()'
CMakeFiles/project.dir/dev/hmmenc_client/main.cpp.o: In function `pqxx::connect_direct::connect_direct(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/usr/local/include/pqxx/connection.hxx:83: undefined reference to `pqxx::connectionpolicy::connectionpolicy(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/local/include/pqxx/connection.hxx:83: undefined reference to `vtable for pqxx::connect_direct'
CMakeFiles/project.dir/dev/hmmenc_client/main.cpp.o: In function `pqxx::connect_direct::~connect_direct()':
/usr/local/include/pqxx/connection.hxx:78: undefined reference to `vtable for pqxx::connect_direct'
/usr/local/include/pqxx/connection.hxx:78: undefined reference to `pqxx::connectionpolicy::~connectionpolicy()'
CMakeFiles/project.dir/dev/hmmenc_client/main.cpp.o: In function `pqxx::basic_connection<pqxx::connect_direct>::~basic_connection()':
/usr/local/include/pqxx/basic_connection.hxx:66: undefined reference to `pqxx::connection_base::close()'
CMakeFiles/project.dir/dev/hmmenc_client/main.cpp.o: In function `pqxx::basic_connection<pqxx::connect_direct>::basic_connection(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/usr/local/include/pqxx/basic_connection.hxx:57: undefined reference to `pqxx::connection_base::init()'
collect2: error: ld returned 1 exit status
gmake[3]: *** [CMakeFiles/project.dir/build.make:95: project] Error 1
gmake[2]: *** [CMakeFiles/Makefile2:68: CMakeFiles/project.dir/all] Error 2
gmake[1]: *** [CMakeFiles/Makefile2:80: CMakeFiles/project.dir/rule] Error 2
gmake: *** [Makefile:118: project] Error 2

有人可以帮助我使用我的 CMakeLists 配置以获得与在 Shell 中执行上述命令相同的结果吗?

更新

我尝试按照下面的 cmets 中的建议使用 FindPQXX.cmake,但我仍然收到错误

CMake Warning at CMakeLists.txt:76 (find_package):
  By not providing "FindFindPQXX.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "FindPQXX",
  but CMake did not find one.

  Could not find a package configuration file provided by "FindPQXX" with any
  of the following names:

    FindPQXXConfig.cmake
    findpqxx-config.cmake

  Add the installation prefix of "FindPQXX" to CMAKE_PREFIX_PATH or set
  "FindPQXX_DIR" to a directory containing one of the above files.  If
  "FindPQXX" provides a separate development package or SDK, be sure it has
  been installed.

为了让事情更清楚一点。假设我的项目树如下所示:

project # Root folder
project/cmake-build-debug # Some cmake code
project/dev
project/dev/hmmenc_client/ # I write my program with all the subfolders and their classes here
project/dev/SEAL_API/ # Some other code to use later

我的CMakeLists.txtFindPQXX.cmake 文件在我的project 根文件夹中。我要编译的main.cppproject/dev/hmmenc_client/

我已经添加了

find_package(FindPQXX)
target_link_libraries(${TARGET_NAME} pqxx)

致我的CMakeFiles.txt。我在这里缺少什么,${TARGET_NAME} 代表什么?我必须编辑FindPQXX.cmake 文件以包含/usr/pgsql-10/include//usr/pgsql-10/lib/,因为这是我的postgres 开发要求。真正的 PostgreSQL 数据库在其他机器上。

我使用标准设置从上面提到的 Github 安装 libpqxx,因此相关文件夹是 /usr/local/include/pqxx//usr/local/lib/

我是 Cmake 世界的新手,所以不要泄露任何信息 :)

【问题讨论】:

标签: c++ postgresql cmake clion libpqxx


【解决方案1】:

要在 CMake 中使用库“foo”,惯用的方法是查找/使用/编写一个 .cmake 文件,该文件使用 CMake 语法和命令来标识 foo 的安装位置在您的操作系统上并为target_link_libraries 设置适当的变量以查找。

我在 github 上找到了一个 libpqxx 的分支,其中恰好包括 FindPQXX.cmake。为了使用它,请在您的主要CMakeLists.txt 中包含以下内容:

find_package(PQXX)
target_link_libraries(${TARGET_NAME} pqxx)

【讨论】:

  • FindXXX.cmake 脚本旨在与find_package(XXX) 一起使用。不要尝试include()他们。
  • 值得注意的是,FindPQXX.cmake 应该被编译到使用它的包中,并且该脚本的目录应该附加到 CMAKE_MODULE_PATH 变量中。
  • @Tsyvarev 你的意思是我必须将FindPQXX.cmake 文件复制到项目文件夹,然后将项目文件夹添加到CMAKE_MODULE_PATH 变量,然后将find_package(FindPQXX)target_link_libraries(${TARGET_NAME} pqxx) 添加到我的CMakeFiles.txt?
  • @TalG:没错。附加目录可以像list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake) 一样完成(如果你将脚本添加到项目的cmake/ 子目录中。
  • @Tsyvarev 我已经完成了,但我一直收到CMake Warning at CMakeLists.txt:76 (find_package): By not providing "FindFindPQXX.cmake.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "FindPQXX.cmake", but CMake did not find one. Could not find a package configuration file provided by "FindPQXX.cmake" with any of the following names: FindPQXX.cmakeConfig.cmake findpqxx.cmake-config.cmake
【解决方案2】:

这是我对 CMakeLists.txt 的工作配置,它在不适用于 FindPQXX.cmake 文件后执行 g++ -std=c++11 -I /usr/local/include/pqxx/ -L /usr/local/lib/ -I /usr/pgsql-10/include/ -L /usr/pgsql-10/lib/ main.cpp -lpqxx -lpq

cmake_minimum_required(VERSION 3.10)
project(project)

# Set flags
set(GCC_COVERAGE_COMPILE_FLAGS "-std=gnu++11")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")

# The version number.
set(project_VERSION_MAJOR 1)
set(project_VERSION_MINOR 0)

# Use a header file for some CMake settings in the source code
configure_file (
        "${PROJECT_SOURCE_DIR}/project_config.h.in"
        "${PROJECT_BINARY_DIR}/project_config.h"
)

# Set some path variables
set(APP_DOMAIN ${PROJECT_SOURCE_DIR}/dev/hmmenc_client)
set(PQXX /usr/local/include/pqxx)
set(PGSQL_10 /usr/pgsql-10/include)

# Find the needed libraries for liniking with -lpqxx and -lpq flags
find_library(PQXX_LIB pqxx)
#message("PQXX_LIB is ${PQXX_LIB}")
find_library(PQ_LIB pq)
#message("PQ_LIB is ${PQ_LIB}")

# Project path includes
include_directories(${PROJECT_BINARY_DIR} 
                    ${APP_DOMAIN}
                    ${PQXX}
                    ${PGSQL_10}
        )
#message("PQXX is ${PQXX}")
#message("PGSQL_10 is ${PGSQL_10}")

# Set source files
set(SOURCE_FILES
        ${APP_DOMAIN}/db/db.h
        ${APP_DOMAIN}/db/db.cpp
        ${APP_DOMAIN}/main.cpp
    )

add_executable(project ${SOURCE_FILES})
# Link the liraries of pqxx and pq to the executable
target_link_libraries(project ${PQXX_LIB} ${PQ_LIB})

我不明白的一件事是我的find_library(PQ_LIB pq)/user/lib64 中找到了pq,我无法将其更改为在/usr/pgsql-10/lib 中查找,它也有我更喜欢使用的文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-15
    • 1970-01-01
    • 2020-03-15
    • 1970-01-01
    • 2019-06-27
    • 2017-11-14
    • 1970-01-01
    • 2012-04-16
    相关资源
    最近更新 更多