Cmake“构建共享库

时间:2015-10-27 20:13:59

标签: build cmake makefile shared-libraries vala

我尝试使用CMake在同一个项目中与自己的库一起构建一个vala应用程序。构建应用程序工作正常,所以我使用以下CMakeLists.txt为项目添加了一个新的“lib”目录:

# Configure precompile
vala_precompile (LIB_VALA_C ${LIB_NAME}
    Session.vala
PACKAGES
    gio-2.0
    gee-0.8
OPTIONS
    --thread
    --vapidir=${CMAKE_SOURCE_DIR}/vapi
    --target-glib 2.32
GENERATE_VAPI
    ${LIB_NAME}
GENERATE_HEADER
    ${CMAKE_PROJECT_NAME}
)

# Add executable
add_library (${LIB_NAME} SHARED ${LIB_VALA_C})

# Set library properties
set_target_properties (${LIB_NAME} PROPERTIES
    OUTPUT_NAME ${LIB_NAME}
    VERSION ${LIB_SOVERSION}.${LIB_VERSION}
    SOVERSION ${LIB_SOVERSION}
)

target_link_libraries (${LIB_NAME} ${LIB_LIBRARIES})

# Installation
install (TARGETS ${LIB_NAME} DESTINATION ${CMAKE_INSTALL_FULL_LIBDIR}/)
install (FILES ${CMAKE_CURRENT_BINARY_DIR}/${LIB_NAME}.pc DESTINATION ${CMAKE_INSTALL_FULL_LIBDIR}/pkgconfig/)
install (FILES ${CMAKE_CURRENT_BINARY_DIR}/${LIB_NAME}.vapi DESTINATION ${CMAKE_INSTALL_FULL_DATAROOTDIR}/vala/vapi/)
install (FILES ${CMAKE_CURRENT_SOURCE_DIR}/${LIB_NAME}.deps DESTINATION ${CMAKE_INSTALL_FULL_DATAROOTDIR}/vala/vapi/)
install (FILES ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}.h DESTINATION ${CMAKE_INSTALL_FULL_INCLUDEDIR}/${LIB_NAME}/)

项目根目录中的CMakeLists.txt包含以下内容:

# Project name
project (drop)

# Minimum requirements of build-system
cmake_minimum_required (VERSION 2.8)
cmake_policy (VERSION 2.6)

# Global configuration
set (DATADIR "${CMAKE_INSTALL_PREFIX}/share")
set (PKGDATADIR "${DATADIR}/${CMAKE_PROJECT_NAME}")
set (GETTEXT_PACKAGE "${CMAKE_PROJECT_NAME}")
set (RELEASE_NAME "${CMAKE_PROJECT_NAME}")
set (VERSION "0.1")
set (VERSION_INFO "Release")
set (PREFIX ${CMAKE_INSTALL_PREFIX})
set (DOLLAR "$")

# Library configuration
set (LIB_VERSION 1.0)
set (LIB_SOVERSION 0)
set (LIB_NAME ${CMAKE_PROJECT_NAME}-${LIB_VERSION})

# Cmake-files
list (APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)

# Configuration file
configure_file (${CMAKE_SOURCE_DIR}/dropd/config.vala.cmake ${CMAKE_SOURCE_DIR}/dropd/config.vala)

# Check for vala
find_package (Vala REQUIRED)
include (ValaVersion)
ensure_vala_version ("0.18" MINIMUM)
include (ValaPrecompile)

# Disable C compiler warnings
add_definitions (-w)

# Set gettext-package
add_definitions (-DGETTEXT_PACKAGE="${CMAKE_PROJECT_NAME}")

# Check for required dependencies
find_package (PkgConfig)
pkg_check_modules (DEPS REQUIRED granite gthread-2.0 gio-2.0 gee-0.8 avahi-gobject avahi-client)

# Link the avahi library
add_definitions (-lavahi)

# Link dependencies
add_definitions (${DEPS_CFLAGS})
add_definitions (${LIB_CFLAGS})
link_libraries (${DEPS_LIBRARIES})
link_directories (${DEPS_LIBRARY_DIRS})
link_directories (${LIB_LIBRARY_DIRS})

# Load directories
add_subdirectory (dropd)
add_subdirectory (lib)
add_subdirectory (po)
add_subdirectory (data)
add_subdirectory (schemas)

现在构建项目时,主应用程序仍然构建没有问题,但是当库的构建开始时,此错误出现(已翻译):

make[2]: *** No rule to make »../lib/drop-1.0«, 
  required by »lib/drop-1.0«.  End.
make[1]: *** [lib/CMakeFiles/drop-1.0.dir/all] Errors 2
make: *** [all] Errors 2

糟糕的是我不熟悉CMake并且没有得到错误消息的含义,也没有导致它的原因。 我已经在另一个项目中将我的CMakeLists.txt文件与它们进行了比较,这个项目也是由“普通”应用程序和一个库构建的,但我找不到使我的代码无效的差异:http://bazaar.launchpad.net/~wingpanel-devs/wingpanel/trunk/files/head:/

如果你能给我一些我在CMake文件中可能遗漏的提示,那将是非常好的。

1 个答案:

答案 0 :(得分:0)

对于那些有同样问题的人:我现在已经解决了这个问题,从${LIB_NAME}行删除了vala_precompile (LIB_VALA_C ${LIB_NAME}。我仍然不确定这里发生了什么,但这个“解决方案”似乎有效。

仍然赞赏更好的方法来解决这个问题。 ;)