我尝试编译简单程序。
main.cpp:
#include <iostream>
#include <pqxx/pqxx>
using namespace std;
int main(int argc, char **argv)
{
pqxx::connection conn("postgresql://root@localhost/company");
return 0;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.12)
project(TestProject)
include_directories("/usr/local/Cellar/gcc/9.2.0/include")
link_directories("/usr/local/Cellar/gcc/9.2.0/lib")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -lpqxx -lpq")
set(CXX_STANDARD_REQUIRED 17)
add_executable(testPQ main.cpp)
target_include_directories(testPQ PRIVATE "/usr/local/Cellar/libpqxx/6.4.5_1/include" "/usr/local/Cellar/libpq/11.4/include")
target_link_directories(testPQ PRIVATE "/usr/local/Cellar/libpqxx/6.4.5_1/lib" "/usr/local/Cellar/libpq/11.4/lib")
target_link_libraries(testPQ "libpqxx.dylib" "libpq.dylib")
我收到以下错误消息:
Undefined symbols for architecture x86_64:
"pqxx::connectionpolicy::connectionpolicy(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)", referenced from:
pqxx::connect_direct::connect_direct(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) in main.cpp.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
make[2]: *** [testPQ] Error 1
make[1]: *** [CMakeFiles/testPQ.dir/all] Error 2
make: *** [all] Error 2
GCC版本:9.2; LIBPQXX版本:6.4.5;作业系统:OS X 10.14; CMake版本:3.15.1
但是CommandLineTools的GCC(Apple LLVM版本10.0.1(clang-1001.0.46.4))将其编译成功。