Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't compile in Centos7 environment #645

Closed
Sunburst7 opened this issue Feb 27, 2023 · 20 comments
Closed

Can't compile in Centos7 environment #645

Sunburst7 opened this issue Feb 27, 2023 · 20 comments

Comments

@Sunburst7
Copy link

I am not a native specker, Please forgive me。
I want to add libpqxx as a third part dependence by CMake。but when i use "cmake .." to compile, there are something wrong like this:

image

here is my project structure:

image

and here is my CMakeLists.txt:

cmake_minimum_required (VERSION 3.8)

project ("CMPlatformServer" LANGUAGES CXX)

include_directories(
    ${PROJECT_SOURCE_DIR}/src
    ${PROJECT_SOURCE_DIR}/src/server
    ${PROJECT_SOURCE_DIR}/third/workflow/_include/
    ${PROJECT_SOURCE_DIR}/third/libpqxx/include/
)

file(GLOB_RECURSE CODE_SRC  "${PROJECT_SOURCE_DIR}/src/*.h"
                            "${PROJECT_SOURCE_DIR}/src/*.cpp"
                            "${PROJECT_SOURCE_DIR}/src/server/*.h"
                            "${PROJECT_SOURCE_DIR}/src/server/*.cpp")

add_subdirectory(${PROJECT_SOURCE_DIR}/third/workflow)
add_subdirectory(${PROJECT_SOURCE_DIR}/third/libpqxx)

add_executable (CMPlatformServer ${CODE_SRC})

set(LIBPQXX_TARGET_DIRS ${PROJECT_SOURCE_DIR}/third/libpqxx/include/pqxx/)
set(LIBPQXX_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/third/libpqxx/include/)

target_link_libraries(CMPlatformServer
    PRIVATE workflow-static pthread ssl crypto
    PUBLIC ${LIBPQXX_TARGET_DIRS}
)

target_include_directories(CMPlatformServer ${LIBPQXX_INCLUDE_DIRS})

how can i currently import libpqxx by CMake?

@Sunburst7
Copy link
Author

in addition, my envirnoment is:
centos7 、c++11、libpqxx 6.3

@KayEss
Copy link
Contributor

KayEss commented Feb 27, 2023

The simplest if you're using cmake is to add pqxx as a subdirectory:

add_subdirectory(third/libqxx)

And then use pqxx in your target_link_libraries

@Sunburst7
Copy link
Author

The simplest if you're using cmake is to add pqxx as a subdirectory:

add_subdirectory(third/libqxx)

And then use pqxx in your target_link_libraries

but i already write it:
image

@KayEss
Copy link
Contributor

KayEss commented Feb 27, 2023

But you also have a load of other things. You only need the add_subdirectory command and then link with pqxx. It is only that

@Sunburst7
Copy link
Author

this time,i simplify my CMakeLists.txt except things for importing workflow

cmake_minimum_required (VERSION 3.8)

project ("CMPlatformServer" LANGUAGES CXX)
MESSAGE(STATUS "Project Directory: ${PROJECT_SOURCE_DIR}")

include_directories(
    ${PROJECT_SOURCE_DIR}/src
    ${PROJECT_SOURCE_DIR}/src/server
    ${PROJECT_SOURCE_DIR}/third/workflow/_include/
)

file(GLOB_RECURSE CODE_SRC  "${PROJECT_SOURCE_DIR}/src/*.h"
                            "${PROJECT_SOURCE_DIR}/src/*.cpp"
                            "${PROJECT_SOURCE_DIR}/src/server/*.h"
                            "${PROJECT_SOURCE_DIR}/src/server/*.cpp")

add_subdirectory(${PROJECT_SOURCE_DIR}/third/workflow)
add_subdirectory(${PROJECT_SOURCE_DIR}/third/libpqxx)

add_executable (CMPlatformServer ${CODE_SRC})

target_link_libraries(CMPlatformServer
    PRIVATE workflow-static pqxx pthread ssl crypto
)

But it still reports errors
image

@tt4g
Copy link
Contributor

tt4g commented Feb 27, 2023

The error is reported because the PostgreSQL library installed on the machine is missing. libpqxx depends on the PostgreSQL library, so PostgreSQL must be installed on your machine.

@Sunburst7
Copy link
Author

The error is reported because the PostgreSQL library installed on the machine is missing. libpqxx depends on the PostgreSQL library, so PostgreSQL must be installed on your machine.

I search for my PostgreSQL and find it:
image

in my linux environment, in /usr directory:
image

@KayEss
Copy link
Contributor

KayEss commented Feb 28, 2023

You'll also need the development packages. Depending on the distribution they can have different names. Maybe something like postgresql-server-dev-14 and libpq-dev

@tt4g
Copy link
Contributor

tt4g commented Feb 28, 2023

Note also that CMake automatically detects PostgreSQL libraries only for PostgreSQL versions that have been released at the time CMake is released.
In this case, set the CMake variable PostgreSQL_ROOT to the location of the PostgreSQL library root directory path (in your case cmake -DPostgreSQL_ROOT=/usr/pgsql-14 (... other command args)).

See also: #624 (comment)

@Sunburst7
Copy link
Author

Note also that CMake automatically detects PostgreSQL libraries only for PostgreSQL versions that have been released at the time CMake is released. In this case, set the CMake variable PostgreSQL_ROOT to the location of the PostgreSQL library root directory path (in your case cmake -DPostgreSQL_ROOT=/usr/pgsql-14 (... other command args)).

See also: #624 (comment)

thank you very much, i have solved this problem by adding "set(PostgreSQL_ROOT "/usr/pgsql-14")"
but when i run "make" in /build directory, an error has occurred:

/usr/bin/ld:  can not find -lpqxx

it seems like cmake don't find target pqxx
do i need to set link_directory ?

@KayEss
Copy link
Contributor

KayEss commented Feb 28, 2023

/usr/bin/ld: can not find -lpqxx

The error makes it look like it's not understanding that -l is a switch and it's trying to link against a file -lpqxx. Are you sure you still have pqxx as the link library in the cmake file?

@Sunburst7
Copy link
Author

/usr/bin/ld: can not find -lpqxx

The error makes it look like it's not understanding that -l is a switch and it's trying to link against a file -lpqxx. Are you sure you still have pqxx as the link library in the cmake file?

yes, i add it in target_link_libraries:

image

@Sunburst7
Copy link
Author

/usr/bin/ld: can not find -lpqxx

The error makes it look like it's not understanding that -l is a switch and it's trying to link against a file -lpqxx. Are you sure you still have pqxx as the link library in the cmake file?

this is my CMakeLists.txt

cmake_minimum_required (VERSION 3.8)

project ("CMPlatformServer" LANGUAGES CXX)
set(CMAKE_CXX_FLAGS "-D_GLIBCXX_USE_CXX11_ABI=0 -g -Werror=return-type")
add_definitions(-std=c++11)

MESSAGE(STATUS "Project Directory: ${PROJECT_SOURCE_DIR}")

set(PostgreSQL_ROOT "/usr/pgsql-14")

link_directories()

include_directories(
    ${PROJECT_SOURCE_DIR}/src
    ${PROJECT_SOURCE_DIR}/src/server
    ${PROJECT_SOURCE_DIR}/third/workflow/_include/
    ${PROJECT_SOURCE_DIR}/third/libpqxx/include/
)

file(GLOB_RECURSE CODE_SRC  "${PROJECT_SOURCE_DIR}/src/*.h"
                            "${PROJECT_SOURCE_DIR}/src/*.cpp"
                            "${PROJECT_SOURCE_DIR}/src/server/*.h"
                            "${PROJECT_SOURCE_DIR}/src/server/*.cpp")

add_subdirectory(${PROJECT_SOURCE_DIR}/third/workflow)
add_subdirectory(${PROJECT_SOURCE_DIR}/third/libpqxx)

add_executable (CMPlatformServer ${CODE_SRC})

target_link_libraries(CMPlatformServer
    PRIVATE workflow-static pqxx pthread ssl crypto
)

@Sunburst7
Copy link
Author

Sunburst7 commented Feb 28, 2023

/usr/bin/ld: can not find -lpqxx

The error makes it look like it's not understanding that -l is a switch and it's trying to link against a file -lpqxx. Are you sure you still have pqxx as the link library in the cmake file?

in build/third/libpqxx/CMakeFiles directory, it doesn't create a .so or .a lib file
i guess that's the resason why cmake can't find -lpqxx

image

@Sunburst7
Copy link
Author

Sunburst7 commented Feb 28, 2023

I have a question about importing thirdpart library.
Do i need to build thirdpart lib before i copy to my project ? 🤔

for example, in my this project:
image
type under codes:

cd third/libpqxx
mkdir build
cd build
cmake ..
make

then i build my whole project

i have this idea because i import thirdpart lib which don't be built yet, so it don't have .a or .so lib file, how can cmake link target with static library or dynamic library ?
when cmake finish building and compiling, in ${PROJECT_SOURCE_DIR}/build cmake create it's own lib file like workflow:
image

@tt4g
Copy link
Contributor

tt4g commented Feb 28, 2023

I have a question about importing thirdpart library.
Do i need to build thirdpart lib before i copy to my project ? 🤔

If third-party libraries are imported with add_subdirectory(), the answer is No.
add_subdirectory() imports CMakeLists.txt in the specified directory as part of the project. CMake target pqxx must be included in the project.
Therefore, target_link_libraries(CMPlatformServer ... pqxx) will mark A as dependent on the CMake target pqxx and pqxx must be built before CMPlatformServer.

You need to remove the CMake build directory once, because CMake generates a cache of variables in the build directory, and the old cache causes the pqxx in the target_link_libraries(CMPlatformServer ... pqxx) to be interpreted as a string instead of CMake target.

@Sunburst7
Copy link
Author

i understand what you said
this time, i remove CMake build and rebuild again: the error still occured

[huhao@localhost build]$ make
[  0%] Built target LINK_HEADERS
[  0%] Building CXX object third/workflow/src/client/CMakeFiles/client.dir/WFDnsClient.cc.o
[  1%] Building CXX object third/workflow/src/client/CMakeFiles/client.dir/WFMySQLConnection.cc.o
[  1%] Building CXX object third/workflow/src/client/CMakeFiles/client.dir/WFConsulClient.cc.o
[  1%] Built target client
[  2%] Building CXX object third/workflow/src/kernel/CMakeFiles/kernel.dir/IOService_linux.cc.o
[  2%] Building C object third/workflow/src/kernel/CMakeFiles/kernel.dir/mpoller.c.o
cc1: 警告:command line option ‘-std=c++11’ is valid for C++/ObjC++ but not for C
[  3%] Building C object third/workflow/src/kernel/CMakeFiles/kernel.dir/poller.c.o
cc1: 警告:command line option ‘-std=c++11’ is valid for C++/ObjC++ but not for C
[  3%] Building C object third/workflow/src/kernel/CMakeFiles/kernel.dir/rbtree.c.o
cc1: 警告:command line option ‘-std=c++11’ is valid for C++/ObjC++ but not for C
[  4%] Building C object third/workflow/src/kernel/CMakeFiles/kernel.dir/msgqueue.c.o
cc1: 警告:command line option ‘-std=c++11’ is valid for C++/ObjC++ but not for C
[  4%] Building C object third/workflow/src/kernel/CMakeFiles/kernel.dir/thrdpool.c.o
cc1: 警告:command line option ‘-std=c++11’ is valid for C++/ObjC++ but not for C
[  4%] Building CXX object third/workflow/src/kernel/CMakeFiles/kernel.dir/CommRequest.cc.o
[  5%] Building CXX object third/workflow/src/kernel/CMakeFiles/kernel.dir/CommScheduler.cc.o
[  5%] Building CXX object third/workflow/src/kernel/CMakeFiles/kernel.dir/Communicator.cc.o
[  6%] Building CXX object third/workflow/src/kernel/CMakeFiles/kernel.dir/Executor.cc.o
[  6%] Building CXX object third/workflow/src/kernel/CMakeFiles/kernel.dir/SubTask.cc.o
[  6%] Built target kernel
[  6%] Building C object third/workflow/src/util/CMakeFiles/util.dir/json_parser.c.o
cc1: 警告:command line option ‘-std=c++11’ is valid for C++/ObjC++ but not for C
[  7%] Building CXX object third/workflow/src/util/CMakeFiles/util.dir/EncodeStream.cc.o
[  7%] Building CXX object third/workflow/src/util/CMakeFiles/util.dir/StringUtil.cc.o
[  8%] Building CXX object third/workflow/src/util/CMakeFiles/util.dir/URIParser.cc.o
[  8%] Built target util
[  9%] Building CXX object third/workflow/src/manager/CMakeFiles/manager.dir/DnsCache.cc.o
[  9%] Building CXX object third/workflow/src/manager/CMakeFiles/manager.dir/RouteManager.cc.o
[  9%] Building CXX object third/workflow/src/manager/CMakeFiles/manager.dir/WFGlobal.cc.o
[ 10%] Building CXX object third/workflow/src/manager/CMakeFiles/manager.dir/UpstreamManager.cc.o
[ 10%] Built target manager
[ 11%] Building CXX object third/workflow/src/algorithm/CMakeFiles/algorithm.dir/DnsRoutine.cc.o
[ 11%] Built target algorithm
[ 12%] Building CXX object third/workflow/src/protocol/CMakeFiles/protocol.dir/PackageWrapper.cc.o
[ 12%] Building CXX object third/workflow/src/protocol/CMakeFiles/protocol.dir/SSLWrapper.cc.o
[ 13%] Building C object third/workflow/src/protocol/CMakeFiles/protocol.dir/dns_parser.c.o
cc1: 警告:command line option ‘-std=c++11’ is valid for C++/ObjC++ but not for C
[ 13%] Building CXX object third/workflow/src/protocol/CMakeFiles/protocol.dir/DnsMessage.cc.o
[ 14%] Building CXX object third/workflow/src/protocol/CMakeFiles/protocol.dir/DnsUtil.cc.o
[ 14%] Building C object third/workflow/src/protocol/CMakeFiles/protocol.dir/http_parser.c.o
cc1: 警告:command line option ‘-std=c++11’ is valid for C++/ObjC++ but not for C
[ 15%] Building CXX object third/workflow/src/protocol/CMakeFiles/protocol.dir/HttpMessage.cc.o
[ 15%] Building CXX object third/workflow/src/protocol/CMakeFiles/protocol.dir/HttpUtil.cc.o
[ 15%] Building C object third/workflow/src/protocol/CMakeFiles/protocol.dir/mysql_stream.c.o
cc1: 警告:command line option ‘-std=c++11’ is valid for C++/ObjC++ but not for C
[ 16%] Building C object third/workflow/src/protocol/CMakeFiles/protocol.dir/mysql_parser.c.o
cc1: 警告:command line option ‘-std=c++11’ is valid for C++/ObjC++ but not for C
[ 16%] Building C object third/workflow/src/protocol/CMakeFiles/protocol.dir/mysql_byteorder.c.o
cc1: 警告:command line option ‘-std=c++11’ is valid for C++/ObjC++ but not for C
[ 17%] Building CXX object third/workflow/src/protocol/CMakeFiles/protocol.dir/MySQLMessage.cc.o
[ 17%] Building CXX object third/workflow/src/protocol/CMakeFiles/protocol.dir/MySQLResult.cc.o
[ 18%] Building CXX object third/workflow/src/protocol/CMakeFiles/protocol.dir/MySQLUtil.cc.o
[ 18%] Building C object third/workflow/src/protocol/CMakeFiles/protocol.dir/redis_parser.c.o
cc1: 警告:command line option ‘-std=c++11’ is valid for C++/ObjC++ but not for C
[ 18%] Building CXX object third/workflow/src/protocol/CMakeFiles/protocol.dir/RedisMessage.cc.o
[ 18%] Built target protocol
[ 18%] Building CXX object third/workflow/src/factory/CMakeFiles/factory.dir/WFGraphTask.cc.o
[ 19%] Building CXX object third/workflow/src/factory/CMakeFiles/factory.dir/DnsTaskImpl.cc.o
[ 19%] Building CXX object third/workflow/src/factory/CMakeFiles/factory.dir/WFTaskFactory.cc.o
[ 20%] Building CXX object third/workflow/src/factory/CMakeFiles/factory.dir/Workflow.cc.o
[ 20%] Building CXX object third/workflow/src/factory/CMakeFiles/factory.dir/HttpTaskImpl.cc.o
[ 21%] Building CXX object third/workflow/src/factory/CMakeFiles/factory.dir/WFResourcePool.cc.o
[ 21%] Building CXX object third/workflow/src/factory/CMakeFiles/factory.dir/WFMessageQueue.cc.o
[ 21%] Building CXX object third/workflow/src/factory/CMakeFiles/factory.dir/FileTaskImpl.cc.o
[ 22%] Building CXX object third/workflow/src/factory/CMakeFiles/factory.dir/MySQLTaskImpl.cc.o
[ 22%] Building CXX object third/workflow/src/factory/CMakeFiles/factory.dir/RedisTaskImpl.cc.o
[ 22%] Built target factory
[ 22%] Building CXX object third/workflow/src/nameservice/CMakeFiles/nameservice.dir/WFNameService.cc.o
[ 23%] Building CXX object third/workflow/src/nameservice/CMakeFiles/nameservice.dir/WFDnsResolver.cc.o
[ 23%] Building CXX object third/workflow/src/nameservice/CMakeFiles/nameservice.dir/WFServiceGovernance.cc.o
[ 24%] Building CXX object third/workflow/src/nameservice/CMakeFiles/nameservice.dir/UpstreamPolicies.cc.o
[ 24%] Built target nameservice
[ 25%] Building CXX object third/workflow/src/server/CMakeFiles/server.dir/WFServer.cc.o
[ 25%] Building CXX object third/workflow/src/server/CMakeFiles/server.dir/WFMySQLServer.cc.o
[ 25%] Built target server
[ 26%] Linking CXX static library ../../../../third/workflow/_lib/libworkflow.a
[ 26%] Built target workflow-static
[ 26%] Building CXX object CMakeFiles/CMPlatformServer.dir/src/server/ServerRouter.cpp.o
[ 26%] Building CXX object CMakeFiles/CMPlatformServer.dir/src/server/ServerUtil.cpp.o
[ 27%] Building CXX object CMakeFiles/CMPlatformServer.dir/src/start.cpp.o
[ 27%] Linking CXX executable CMPlatformServer
/usr/bin/ld: can not find -lpqxx

but i notice that it does't build pqxx target before link CMPlatformServer target; but another thirdpart target called workflow has built successfully
so it can't find pqxx target.
do i have mistake when i use “target_link_libraries” ?
here is my CMakeLists.txt:

cmake_minimum_required (VERSION 3.8)

project ("CMPlatformServer" LANGUAGES CXX)
add_definitions(-std=c++11)

MESSAGE(STATUS "Project Directory: ${PROJECT_SOURCE_DIR}")

set(PostgreSQL_ROOT "/usr/pgsql-14")

include_directories(
    ${PROJECT_SOURCE_DIR}/src
    ${PROJECT_SOURCE_DIR}/src/server
    ${PROJECT_SOURCE_DIR}/third/workflow/_include/
    ${PROJECT_SOURCE_DIR}/third/libpqxx/include/
)

file(GLOB_RECURSE CODE_SRC  "${PROJECT_SOURCE_DIR}/src/*.h"
                            "${PROJECT_SOURCE_DIR}/src/*.cpp"
                            "${PROJECT_SOURCE_DIR}/src/server/*.h"
                            "${PROJECT_SOURCE_DIR}/src/server/*.cpp")

add_subdirectory(${PROJECT_SOURCE_DIR}/third/workflow)
add_subdirectory(${PROJECT_SOURCE_DIR}/third/libpqxx)

add_executable (CMPlatformServer ${CODE_SRC})

target_link_libraries(CMPlatformServer
    PRIVATE workflow-static pthread ssl crypto
    PRIVATE pqxx
)

@KayEss
Copy link
Contributor

KayEss commented Feb 28, 2023

You can see in the build output that it isn't building pqxx. Maybe the extra PRIVATE you have in the target_link_libraries is confusing cmake.

@Sunburst7
Copy link
Author

You can see in the build output that it isn't building pqxx. Maybe the extra PRIVATE you have in the target_link_libraries is confusing cmake.

I turn to version7.6 and switch c++ compile version to C++17. and finally success! 🎉🎉
I guess libpqxx 6.x may has some problem when compiling.
my CMakeLists.txt don't amend anything, that prove it is works

thank you so much to help me solve problems 😘😘

@jtv
Copy link
Owner

jtv commented Feb 28, 2023

Thanks @tt4g & @KayEss - I had nothing to contribute to this one!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants