CMake for mysqlpp
(1) By Jan Skoviera (rossum26) on 2022-06-13 15:19:27 [source]
So, I successfully compiled mysql++ on both linux and windows using cmake. The scripts assume, that if you are on linux, you just plainly installed the libraries (mysql and mysqlclient) and if you are on windows you use vcpkg. It is very primitive so far but works. The only required change to the library is to remove MS extensions (__declspec(dllimport) and __declspec(dllexport)), cmake will take care of this, so if you leave it there, the compiler will complain about inconsistent import/export types. So one has to either delete the __declspec stuff (so that MYSQLPP_EXPORT resolves to nothing) or simply remove every installation of MYSQLPP_EXPORT macro. I also modified created a stable mysql++.h that includes config.h generated by cmake from config.h.in. Here are the two cmake lists: CMakeLists.txt in the root directory:
cmake_minimum_required(VERSION 3.16)
project(mysqlpp VERSION 3.3.1)
configure_file("${PROJECT_SOURCE_DIR}/config.h.in" "${PROJECT_SOURCE_DIR}/lib/config.h")
execute_process(COMMAND "perl -w ${PROJECT_SOURCE_DIR}/lib/querydef.pl"
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
)
execute_process(COMMAND "perl -w ${PROJECT_SOURCE_DIR}/lib/ssqls.pl"
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
)
if(SINGLE_THREAD)
message("Using single-thread configuration")
message("you will not be able to user threads")
message("set SINGLE_THREAD to 0 if this is not what you intended")
else()
message("Using multithread configuration")
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads REQUIRED)
endif()
if(UNIX)
find_path(mysql mysql REQUIRED)
find_library(mysqlclient mysqlclient CONFIG REQUIRED)
elseif(WIN32)
set(VCPKG_TARGET_TRIPLET x64-windows)
if(VCPKG_ROOT)
message("VCPKG_ROOT is ${VCPKG_ROOT}")
else()
set(VCPKG_ROOT "C:/Program Files/vcpkg")
message("VCPKG_ROOT not set, assuming ${VCPKG_ROOT}")
endif()
include(${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake)
find_package(libmysql CONFIG REQUIRED)
endif()
add_compile_definitions(MYSQLPP_MYSQL_HEADERS_BURIED)
add_subdirectory(lib)
and CMakeLists.txt in the lib directory:
set(MYSQLPP_SOURCES
beemutex.cpp
cmdline.cpp
connection.cpp
cpool.cpp
datetime.cpp
dbdriver.cpp
field_names.cpp
field_types.cpp
manip.cpp
myset.cpp
mysql++.cpp
mystring.cpp
null.cpp
options.cpp
qparms.cpp
query.cpp
result.cpp
row.cpp
scopedconnection.cpp
sql_buffer.cpp
sqlstream.cpp
ssqls2.cppset(MYSQLPP_SOURCES
beemutex.cpp
cmdline.cpp
connection.cpp
cpool.cpp
datetime.cpp
dbdriver.cpp
field_names.cpp
field_types.cpp
manip.cpp
myset.cpp
mysql++.cpp
mystring.cpp
null.cpp
options.cpp
qparms.cpp
query.cpp
result.cpp
row.cpp
scopedconnection.cpp
sql_buffer.cpp
sqlstream.cpp
ssqls2.cpp
stadapter.cpp
tcp_connection.cpp
transaction.cpp
type_info.cpp
uds_connection.cpp
utility.cpp
vallist.cpp
wnp_connection.cpp
)
add_library(mysqlpp SHARED ${MYSQLPP_SOURCES})
if(WIN32)
target_link_libraries(mysqlpp PUBLIC
libmysql
)
else()
target_link_libraries(mysqlpp PUBLIC
mysqlclient
)
endif()
stadapter.cpp
tcp_connection.cpp
transaction.cpp
type_info.cpp
uds_connection.cpp
utility.cpp
vallist.cpp
wnp_connection.cpp
)
add_library(mysqlpp SHARED ${MYSQLPP_SOURCES})
if(WIN32)
target_link_libraries(mysqlpp PUBLIC
libmysql
)
else()
target_link_libraries(mysqlpp PUBLIC
mysqlclient
)
endif()
I didn't include the tests for now.
(2) By NObodyGX on 2022-08-18 08:52:20 in reply to 1 [link] [source]
Thank you. It's really useful!!!I changed it a little, which makes input easier in linux.
CMakeLists.txt in the root directory:
cmake_minimum_required(VERSION 3.16)
project(mysqlpp VERSION 3.2.3)
configure_file("${PROJECT_SOURCE_DIR}/config.h.in" "${PROJECT_SOURCE_DIR}/lib/config.h")
configure_file("${PROJECT_SOURCE_DIR}/lib/mysql++.h.in" "${PROJECT_SOURCE_DIR}/lib/mysql++.h")
execute_process(COMMAND "perl -w ${PROJECT_SOURCE_DIR}/lib/querydef.pl"
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})
execute_process(COMMAND "perl -w ${PROJECT_SOURCE_DIR}/lib/ssqls.pl"
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})
find_path(mysql mysql REQUIRED)
find_library(mysqlclient mysqlclient CONFIG REQUIRED)
add_compile_definitions(MYSQLPP_MYSQL_HEADERS_BURIED)
add_subdirectory(lib)
CMakeLists.txt in the lib directory:
aux_source_directory(. MYSQLPP_SOURCES)
add_library(mysqlpp SHARED ${MYSQLPP_SOURCES})
target_link_libraries(mysqlpp PUBLIC mysqlclient)