From 7080918350c5203526079aab8e8089d145ca745e Mon Sep 17 00:00:00 2001 From: ShiXiaodi Date: Tue, 17 Oct 2023 15:19:49 +0800 Subject: [PATCH] Fix compilation errors when using CMake to add a project through add_subdirectory. --- .gitignore | 3 +++ CMakeLists.txt | 44 ++++++++++++++++++++++---------------- backend/zint.h | 2 ++ backend_qt/CMakeLists.txt | 2 +- frontend/CMakeLists.txt | 4 ++-- frontend_qt/CMakeLists.txt | 2 +- 6 files changed, 34 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index 46c37bd9..135d671c 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,6 @@ win32/Debug win32/Release win32/x64 extern/* + +.vscode/ +out/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 967c4bb8..ab167e89 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,18 +15,20 @@ set(ZINT_VERSION "${ZINT_VERSION_MAJOR}.${ZINT_VERSION_MINOR}.${ZINT_VERSION_REL add_definitions(-DZINT_VERSION=\"${ZINT_VERSION}\") -set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules") +set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules") -option(ZINT_DEBUG "Set debug compile flags" OFF) -option(ZINT_NOOPT "Set no optimize compile flags" OFF) -option(ZINT_SANITIZE "Set sanitize compile/link flags" OFF) -option(ZINT_TEST "Set test compile flag" OFF) -option(ZINT_COVERAGE "Set code coverage flags" OFF) -option(ZINT_SHARED "Build shared library" ON) -option(ZINT_STATIC "Build static library" OFF) -option(ZINT_USE_PNG "Build with PNG support" ON) -option(ZINT_USE_QT "Build with Qt support" ON) -option(ZINT_QT6 "If ZINT_USE_QT, use Qt6" OFF) +option(ZINT_DEBUG "Set debug compile flags" OFF) +option(ZINT_NOOPT "Set no optimize compile flags" OFF) +option(ZINT_SANITIZE "Set sanitize compile/link flags" OFF) +option(ZINT_TEST "Set test compile flag" OFF) +option(ZINT_COVERAGE "Set code coverage flags" OFF) +option(ZINT_SHARED "Build shared library" ON) +option(ZINT_STATIC "Build static library" OFF) +option(ZINT_USE_PNG "Build with PNG support" ON) +option(ZINT_USE_QT "Build with Qt support" ON) +option(ZINT_QT6 "If ZINT_USE_QT, use Qt6" OFF) +option(ZINT_UNINSTALL "Add uninstall target" ON) +option(ZINT_FRONTEND "Build frontend" ON) if(NOT ZINT_SHARED AND NOT ZINT_STATIC) message(SEND_ERROR "Either ZINT_SHARED or ZINT_STATIC or both must be set") @@ -143,7 +145,9 @@ if(NOT HAVE_GETOPT) endif() add_subdirectory(backend) -add_subdirectory(frontend) +if(ZINT_FRONTEND) + add_subdirectory(frontend) +endif() if(NOT ZINT_USE_QT) message(STATUS "Qt support was disabled for this build") @@ -184,13 +188,15 @@ else() endif() endif() -configure_file( - "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in" - "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake" - IMMEDIATE @ONLY) - -add_custom_target(uninstall - "${CMAKE_COMMAND}" -P "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake") +if(ZINT_UNINSTALL) + configure_file( + "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake" + IMMEDIATE @ONLY) + + add_custom_target(uninstall + "${CMAKE_COMMAND}" -P "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake") +endif() # staniek: don't install if(DATA_INSTALL_DIR) diff --git a/backend/zint.h b/backend/zint.h index 6cbebba1..1e1b1120 100644 --- a/backend/zint.h +++ b/backend/zint.h @@ -372,6 +372,8 @@ extern "C" { # else # define ZINT_EXTERN extern # endif +#elif defined(__GNUC__) && __GNUC__ >= 4 +# define ZINT_EXTERN extern __attribute__((visibility("default"))) #else # define ZINT_EXTERN extern #endif diff --git a/backend_qt/CMakeLists.txt b/backend_qt/CMakeLists.txt index 1ac488f2..bf4cd1dc 100644 --- a/backend_qt/CMakeLists.txt +++ b/backend_qt/CMakeLists.txt @@ -17,7 +17,7 @@ add_library(${PROJECT_NAME} STATIC ${QZint_SRCS}) set_target_properties(${PROJECT_NAME} PROPERTIES SOVERSION "${ZINT_VERSION_MAJOR}.${ZINT_VERSION_MINOR}" VERSION ${ZINT_VERSION}) -target_include_directories(${PROJECT_NAME} PUBLIC "${CMAKE_SOURCE_DIR}/backend") +target_include_directories(${PROJECT_NAME} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../backend") if(ZINT_SHARED) target_link_libraries(${PROJECT_NAME} zint) diff --git a/frontend/CMakeLists.txt b/frontend/CMakeLists.txt index b5b6d98f..07d176eb 100644 --- a/frontend/CMakeLists.txt +++ b/frontend/CMakeLists.txt @@ -12,7 +12,7 @@ if(WIN32) target_sources(${PROJECT_NAME} PRIVATE zint.rc) endif() -target_include_directories(${PROJECT_NAME} PUBLIC "${CMAKE_SOURCE_DIR}/backend") +target_include_directories(${PROJECT_NAME} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../backend") set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME "zint") @@ -27,7 +27,7 @@ endif() install(TARGETS ${PROJECT_NAME} DESTINATION "${BIN_INSTALL_DIR}" RUNTIME) if(UNIX) - install(FILES "${CMAKE_SOURCE_DIR}/docs/zint.1" DESTINATION "${MAN_INSTALL_DIR}/man1" COMPONENT doc) + install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/../docs/zint.1" DESTINATION "${MAN_INSTALL_DIR}/man1" COMPONENT doc) endif() if(ZINT_TEST) diff --git a/frontend_qt/CMakeLists.txt b/frontend_qt/CMakeLists.txt index 767d068d..a3e0f901 100644 --- a/frontend_qt/CMakeLists.txt +++ b/frontend_qt/CMakeLists.txt @@ -46,7 +46,7 @@ if(WIN32) target_sources(${PROJECT_NAME} PRIVATE res/qtZint.rc) endif() -target_include_directories(${PROJECT_NAME} PUBLIC "${CMAKE_SOURCE_DIR}/backend" "${CMAKE_SOURCE_DIR}/backend_qt") +target_include_directories(${PROJECT_NAME} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../backend" "${CMAKE_CURRENT_SOURCE_DIR}/../backend_qt") if(ZINT_SHARED) target_link_libraries(${PROJECT_NAME} zint)