CLion 1.2 中用于 PostgreSQL 的 CMake Windows 10

CMake for PostgreSQL in CLion 1.2 Windows 10

无法在 CLion 1.2 EAP 中使用 PostgreSQL 构建 C++ 文件。

CMake 文件:

cmake_minimum_required(VERSION 3.3)
project(PostgreSQLTest)

find_package(PostgreSQL REQUIRED)
set(PostgreSQL_INCLUDE_DIRS, "C:\Program Files\PostgreSQL\9.4\include")
include_directories (${PostgreSQL_INCLUDE_DIRS})

set(SOURCES main.cpp)
add_executable(PostgreSQLTest ${SOURCES})
set(PostgreSQL_LIBRARIES, "C:\Program Files\PostgreSQL\9.4\lib")
target_link_libraries(PostgreSQLTest ${PostgreSQL_LIBRARIES})

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES
    CMakeLists.txt
    main.cpp)

构建消息:

"C:\Program Files (x86)\JetBrains\CLion 142.4859.12\bin\cmake\bin\cmake.exe" --build C:\Users\darin\.CLion12\system\cmake\generated\f846c544\f846c544\Debug --target PostgreSQLTest -- -j 8
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/darin/.CLion12/system/cmake/generated/f846c544/f846c544/Debug
[ 50%] Linking CXX executable PostgreSQLTest.exe
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: cannot find -llibpq
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [PostgreSQLTest.exe] Error 1
CMakeFiles\PostgreSQLTest.dir\build.make:96: recipe for target 'PostgreSQLTest.exe' failed
mingw32-make.exe[2]: *** [CMakeFiles/PostgreSQLTest.dir/all] Error 2
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/PostgreSQLTest.dir/all' failed
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/PostgreSQLTest.dir/rule' failed
Makefile:117: recipe for target 'PostgreSQLTest' failed
mingw32-make.exe[1]: *** [CMakeFiles/PostgreSQLTest.dir/rule] Error 2
mingw32-make.exe: *** [PostgreSQLTest] Error 2

有没有人有过这个版本的经验?

应用评论 2015-09-21 的更改后进行编辑

cmake_minimum_required(VERSION 3.3)
project(PostgreSQLTest)

set(PostgreSQL_ROOT, "C:\Program Files\PostgreSQL\9.4")
find_package(PostgreSQL REQUIRED)

set(SOURCES main.cpp)
add_executable(PostgreSQLTest ${SOURCES})

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES
    CMakeLists.txt
    main.cpp)

错误:

"C:\Program Files (x86)\JetBrains\CLion 142.4859.12\bin\cmake\bin\cmake.exe" --build C:\Users\darin\.CLion12\system\cmake\generated\f846c544\f846c544\Debug --target PostgreSQLTest -- -j 8
[ 50%] Building CXX object CMakeFiles/PostgreSQLTest.dir/main.cpp.obj
C:\Users\darin\ClionProjects\PostgreSQLTest\main.cpp:2:22: fatal error: libpq-fe.h: No such file or directory
#include "libpq-fe.h"
                   ^
compilation terminated.
mingw32-make.exe[3]: *** [CMakeFiles/PostgreSQLTest.dir/main.cpp.obj] Error 1
CMakeFiles\PostgreSQLTest.dir\build.make:61: recipe for target 'CMakeFiles/PostgreSQLTest.dir/main.cpp.obj' failed
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/PostgreSQLTest.dir/all' failed
mingw32-make.exe[2]: *** [CMakeFiles/PostgreSQLTest.dir/all] Error 2
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/PostgreSQLTest.dir/rule' failed
mingw32-make.exe[1]: *** [CMakeFiles/PostgreSQLTest.dir/rule] Error 2
Makefile:117: recipe for target 'PostgreSQLTest' failed
mingw32-make.exe: *** [PostgreSQLTest] Error 2

我已经确认 libpq-fh.h 确实存在于 C:\Program Files\PostgreSQL.4\include 中,所以我的 Cmake 文件中肯定还有一些不正确的东西阻止了构建.

我是否缺少构建所需的 target_link_libraries()

应用建议的更改后编辑 2

cmake_minimum_required(VERSION 3.3)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

project(PostgreSQLTest)

set(PostgreSQL_ROOT "C:\Program Files\PostgreSQL\9.4")
find_package(PostgreSQL REQUIRED)

set(SOURCES main.cpp)
add_executable(PostgreSQLTest ${SOURCES})

set(PostgreSQL_LIBRARIES "C:\Program Files\PostgreSQL\9.4\lib")
target_link_libraries(PostgreSQLTest ${PostgreSQL_LIBRARIES})


set(SOURCE_FILES
    CMakeLists.txt
    main.cpp)

错误:

"C:\Program Files (x86)\JetBrains\CLion 142.4859.12\bin\cmake\bin\cmake.exe" --build C:\Users\darin\.CLion12\system\cmake\generated\f846c544\f846c544\Debug --target all -- -j 8
[ 50%] Building CXX object CMakeFiles/PostgreSQLTest.dir/main.cpp.obj
C:\Users\darin\ClionProjects\PostgreSQLTest\main.cpp:2:22: fatal error: libpq-fe.h: No such file or directory
#include "libpq-fe.h"
                     ^
compilation terminated.
mingw32-make.exe[2]: *** [CMakeFiles/PostgreSQLTest.dir/main.cpp.obj] Error 1
mingw32-make.exe[1]: *** [CMakeFiles/PostgreSQLTest.dir/all] Error 2
mingw32-make.exe: *** [all] Error 2
CMakeFiles\PostgreSQLTest.dir\build.make:61: recipe for target 'CMakeFiles/PostgreSQLTest.dir/main.cpp.obj' failed
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/PostgreSQLTest.dir/all' failed
Makefile:82: recipe for target 'all' failed

我进行了建议的更改,尝试了几种不同的编译顺序,但仍然失败。

有什么想法吗?

2015 年 10 月 5 日发表评论后进行的编辑

cmake_minimum_required(VERSION 3.3)
project(PostgreSQLTest)

# For *modify* CMAKE_CXX_FLAGS, this should come after project() call.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

# Next line is not required, without it given path will be checked too.
#set(PostgreSQL_ROOT "C:\Program Files (x86)\PostgreSQL\9.4")
#set(PostgreSQL ROOT "C:\MinGW\compiledLibs\psql\postgresql-9.6devel\tmp_install\usr\local\pgsql")

set(PostgreSQL ROOT "C:\MinGW\msys\1.0\local\pgsql")
find_package(PostgreSQL REQUIRED)
message("PostgreSQL_LIBRARY_DIRS: ${PostgreSQL_LIBRARY_DIRS}")

# Now variables PostgreSQL_INCLUDE_DIRS, PostgreSQL_LIBRARIES are set. Just use them.
include_directories (${PostgreSQL_INCLUDE_DIRS})
link_directories(${PostgreSQL_LIBRARY_DIRS})

set(SOURCES main.cpp)
add_executable(PostgreSQLTest ${SOURCES})
target_link_libraries(PostgreSQLTest ${PostgreSQL_LIBRARIES})

set(SOURCE_FILES
    CMakeLists.txt
    main.cpp)

我没有找到任何表明使用了 posgres 库的信息。

"C:\Program Files (x86)\JetBrains\CLion 142.4859.12\bin\cmake\bin\cmake.exe" --build C:\Users\darin\.CLion12\system\cmake\generated\f846c544\f846c544\Debug --target all -- -j 8
[ 50%] Building CXX object CMakeFiles/PostgreSQLTest.dir/main.cpp.obj
[100%] Linking CXX executable PostgreSQLTest.exe
CMakeFiles\PostgreSQLTest.dir/objects.a(main.cpp.obj): In function `Z9CloseConnP7pg_conn':
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:9: undefined reference to `PQfinish'
CMakeFiles\PostgreSQLTest.dir/objects.a(main.cpp.obj): In function `Z9ConnectDBv':
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:19: undefined reference to `PQconnectdb'
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:22: undefined reference to `PQstatus'
CMakeFiles\PostgreSQLTest.dir/objects.a(main.cpp.obj): In function `Z19CreateEmployeeTableP7pg_conn':
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:39: undefined reference to `PQexec'
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:41: undefined reference to `PQresultStatus'
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:44: undefined reference to `PQclear'
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:51: undefined reference to `PQclear'
CMakeFiles\PostgreSQLTest.dir/objects.a(main.cpp.obj): In function `Z17InsertEmployeeRecP7pg_connPKcS2_':
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:66: undefined reference to `PQexec'
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:68: undefined reference to `PQresultStatus'
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:71: undefined reference to `PQclear'
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:78: undefined reference to `PQclear'
CMakeFiles\PostgreSQLTest.dir/objects.a(main.cpp.obj): In function `Z16FetchEmployeeRecP7pg_conn':
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:88: undefined reference to `PQexec'
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:90: undefined reference to `PQresultStatus'
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:93: undefined reference to `PQclear'
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:98: undefined reference to `PQclear'
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:101: undefined reference to `PQexec'
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:102: undefined reference to `PQresultStatus'
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:105: undefined reference to `PQclear'
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:110: undefined reference to `PQclear'
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:112: undefined reference to `PQexec'
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:114: undefined reference to `PQresultStatus'
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:117: undefined reference to `PQclear'
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:122: undefined reference to `PQnfields'
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:128: undefined reference to `PQfname'
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:135: undefined reference to `PQgetvalue'
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:132: undefined reference to `PQntuples'
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:139: undefined reference to `PQclear'
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:142: undefined reference to `PQexec'
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:143: undefined reference to `PQclear'
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:146: undefined reference to `PQexec'
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:149: undefined reference to `PQclear'
CMakeFiles\PostgreSQLTest.dir/objects.a(main.cpp.obj): In function `Z20RemoveAllEmployeeRecP7pg_conn':
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:156: undefined reference to `PQexec'
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:158: undefined reference to `PQresultStatus'
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:161: undefined reference to `PQclear'
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:168: undefined reference to `PQclear'
CMakeFiles\PostgreSQLTest.dir/objects.a(main.cpp.obj): In function `Z17DropEmployeeTableP7pg_conn':
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:175: undefined reference to `PQexec'
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:177: undefined reference to `PQresultStatus'
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:180: undefined reference to `PQclear'
C:/Users/darin/ClionProjects/PostgreSQLTest/main.cpp:187: undefined reference to `PQclear'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\PostgreSQLTest.dir\build.make:96: recipe for target 'PostgreSQLTest.exe' failed
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/PostgreSQLTest.dir/all' failed
Makefile:82: recipe for target 'all' failed
mingw32-make.exe[2]: *** [PostgreSQLTest.exe] Error 1
mingw32-make.exe[1]: *** [CMakeFiles/PostgreSQLTest.dir/all] Error 2
mingw32-make.exe: *** [all] Error 2

正确CMakeLists.txt:

cmake_minimum_required(VERSION 3.3)
project(PostgreSQLTest)

# For *modify* CMAKE_CXX_FLAGS, this should come after project() call.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

# Next line is not required, without it given path will be checked too.
set(PostgreSQL_ROOT "C:\Program Files\PostgreSQL\9.4")
find_package(PostgreSQL REQUIRED)
# Now variables PostgreSQL_INCLUDE_DIRS, PostgreSQL_LIBRARIES, PostgreSQL_LIBRARY_DIRS are set. Just use them.

include_directories (${PostgreSQL_INCLUDE_DIRS})
link_directories(${PostgreSQL_LIBRARY_DIRS})

set(SOURCES main.cpp)
add_executable(PostgreSQLTest ${SOURCES})
target_link_libraries(PostgreSQLTest ${PostgreSQL_LIBRARIES})

set(SOURCE_FILES
    CMakeLists.txt
    main.cpp)

find_package(PostgreSQL) 调用的主要(但不仅仅是)目的是设置变量 PostgreSQL_INCLUDE_DIRSPostgreSQL_LIBRARIESPostgreSQL_LIBRARY_DIRS(以及其他一些变量)。之后无需手动设置这些变量,直接使用即可。

只需使用这个,在 mac.

上测试过
cmake_minimum_required(VERSION 3.6)
project(postgres)


set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

add_custom_target(postgres COMMAND make -C ${postgres_SOURCE_DIR})

cmake done