【发布时间】:2015-09-09 13:58:43
【问题描述】:
我正在尝试了解如何使用 CMake 构建 C++ 项目。 我不熟悉C++。 我正在开发 mac Os X,我也想在 Linux 上分享该应用程序。
这是我的项目目录架构。你可以在这里找到github
我用静态和共享库做了一个程序,看看它是如何工作的。 我读到了 shared and static libraries ,here on stackoverflow 和 Cmake 的东西 1 和 2
最初我将所有源合并到一个可执行文件中。它运作良好。 但是共享的目标不是被打包,不是吗? 它应该是按位置访问的。或者当我在安装构建下移动我的 lib 和 bin 时。该程序仍在运行。太好了,但我怀疑它应该像这样工作。 我的意思是如果我移动我的共享库,程序应该不再工作了。
所以不要做,
#ADD_EXECUTABLE(ChimericGenomeMaker "${SOURCES_MAIN}" "${SOURCES_STATIC}" "${SOURCES_SHARED}")
我正在尝试使用 TARGET_LINK_LIBRARIES,也许是 (LINK_DIRECTORIES):
TARGET_LINK_LIBRARIES(ChimericGenomeMaker "calculMean" ) TARGET_LINK_LIBRARIES(ChimericGenomeMaker "calculSum" )
但它不起作用。
./install/bin/ChimericGenomeMaker 4
dyld: Library not loaded: libcalculMean.dylib
Referenced from: /Users/JP/Desktop/ChimericGenomeMaker/./install/bin/ChimericGenomeMaker
Reason: image not found
Trace/BPT trap: 5
使用 otool -L ,告诉我下面的内容:
./install/bin/ChimericGenomeMaker:
libcalculMean.dylib (compatibility version 0.0.0, current version 0.0.0)
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 120.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1213.0.0)
好吧,我的 dylib 的位置存在问题......我的第一个想法(合并所有可执行文件,无论如何它们来自的库)是错误的想法吗? 什么是好的“框架”?
最后一个问题,不管是共享库还是静态库,如果我移动了我的 lib 目录,程序会继续正确运行吗?
谢谢。
这是我的 CmakeLists.txt [已更新]
#cmake -G "Eclipse CDT4 - Unix Makefiles" ..
#SET (CMAKE_CXX_COMPILER "/usr/bin/g++")
IF (${APPLE})
MESSAGE(STATUS "WELCOME TO APPLE")
ENDIF()
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(ChimericGenomeMaker)
SET (ChimericGenomeMaker_VERSION_MAJOR 1)
SET (ChimericGenomeMaker_VERSION_MINOR 0)
#used by makefile users in order to install software at non-default location
#make DESTDIR=/home/john install --> prepended with the DESTDIR value
SET(CMAKE_INSTALL_PREFIX install)
#SET(CMAKE_SKIP_BUILD_RPATH true )
#Can manually add the sources using the set command as follows:
FILE(GLOB SOURCES_MAIN "src/main/*.cpp")
INCLUDE_DIRECTORIES("${PROJECT_SOURCE_DIR}/include/main")
#Generate the shared library from the sources
FILE(GLOB SOURCES_SHARED "src/shared/*.cpp")
INCLUDE_DIRECTORIES("${PROJECT_SOURCE_DIR}/include/shared")
ADD_LIBRARY(calculMean SHARED "${SOURCES_SHARED}")
INSTALL(TARGETS calculMean DESTINATION "lib/shared")
#MACOSX_RPATH is not specified for the following targets calcuMean
#Generate the static library from the sources
FILE(GLOB SOURCES_STATIC "src/static/*.cpp")
INCLUDE_DIRECTORIES("${PROJECT_SOURCE_DIR}/include/static")
ADD_LIBRARY(calculSum STATIC "${SOURCES_STATIC}")
INSTALL(TARGETS calculSum DESTINATION "lib/static")
# Use, i.e. don't skip the full RPATH for the build tree
# SET(CMAKE_SKIP_BUILD_RPATH FALSE)
# When building, don't use the install RPATH already (but later on when installing)
#SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
#SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib/shared")
# add the automatically determined parts of the RPATH which point to directories outside the build tree to the install RPATH
#SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
# the RPATH to be used when installing, but only if it's not a system directory
#LIST(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/lib/shared" isSystemDir)
# IF("${isSystemDir}" STREQUAL "-1")
# SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib/shared")
# ENDIF("${isSystemDir}" STREQUAL "-1")
#Build a stand alone program
ADD_EXECUTABLE(ChimericGenomeMaker "${SOURCES_SHARED}" "${SOURCES_MAIN}" )# "${SOURCES_STATIC}"
#TARGET_LINK_LIBRARIES(ChimericGenomeMaker calculMean)
TARGET_LINK_LIBRARIES(ChimericGenomeMaker calculSum)
INSTALL(TARGETS ChimericGenomeMaker DESTINATION bin)
ADD_CUSTOM_COMMAND(TARGET calculMean
POST_BUILD
COMMAND "${CMAKE_SOURCE_DIR}/bash/post-build.sh" "HELLO YOU!"
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
COMMENT "Post-build is Running" VERBATIM
)
CMAKE 的输出:
./build.sh
-- Custom Bash Cleaning
-- Old Install Directory Deleted with sucess
-- WELCOME TO APPLE
-- The C compiler identification is AppleClang 6.1.0.6020053
-- The CXX compiler identification is AppleClang 6.1.0.6020053
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- INSTALL_NAME_DIR:
-- CMAKE_BINARY_DIR: /Users/JP/Desktop/ChimericGenomeMaker/build
-- CMAKE_CURRENT_BINARY_DIR: /Users/JP/Desktop/ChimericGenomeMaker/build
-- CMAKE_SOURCE_DIR: /Users/JP/Desktop/ChimericGenomeMaker
-- CMAKE_CURRENT_SOURCE_DIR: /Users/JP/Desktop/ChimericGenomeMaker
-- PROJECT_BINARY_DIR: /Users/JP/Desktop/ChimericGenomeMaker/build
-- PROJECT_SOURCE_DIR: /Users/JP/Desktop/ChimericGenomeMaker
-- EXECUTABLE_OUTPUT_PATH:
-- LIBRARY_OUTPUT_PATH:
-- CMAKE_MODULE_PATH:
-- CMAKE_COMMAND: /usr/local/Cellar/cmake/3.2.1/bin/cmake
-- CMAKE_ROOT: /usr/local/Cellar/cmake/3.2.1/share/cmake
-- CMAKE_CURRENT_LIST_FILE: /Users/JP/Desktop/ChimericGenomeMaker/CMakeLists.txt
-- CMAKE_CURRENT_LIST_LINE: 122
-- CMAKE_INCLUDE_PATH:
-- CMAKE_LIBRARY_PATH:
-- CMAKE_SYSTEM: Darwin-14.4.0
-- CMAKE_SYSTEM_NAME: Darwin
-- CMAKE_SYSTEM_VERSION: 14.4.0
-- CMAKE_SYSTEM_PROCESSOR: x86_64
-- UNIX: 1
-- WIN32:
-- APPLE: 1
-- MINGW:
-- CYGWIN:
-- BORLAND:
-- MSVC:
-- MSVC_IDE:
-- MSVC60:
-- MSVC70:
-- MSVC71:
-- MSVC80:
-- CMAKE_COMPILER_2005:
-- CMAKE_SKIP_RULE_DEPENDENCY:
-- CMAKE_SKIP_INSTALL_ALL_DEPENDENCY:
-- CMAKE_SKIP_RPATH: NO
-- CMAKE_VERBOSE_MAKEFILE: FALSE
-- CMAKE_SUPPRESS_REGENERATION:
-- CMAKE_C_FLAGS:
-- CMAKE_CXX_FLAGS:
-- CMAKE_BUILD_TYPE:
-- BUILD_SHARED_LIBS:
-- CMAKE_C_COMPILER: /Library/Developer/CommandLineTools/usr/bin/cc
-- CMAKE_CXX_COMPILER: /Library/Developer/CommandLineTools/usr/bin/c++
-- CMAKE_COMPILER_IS_GNUCC:
-- CMAKE_COMPILER_IS_GNUCXX :
-- CMAKE_AR: /Library/Developer/CommandLineTools/usr/bin/ar
-- CMAKE_RANLIB: /Library/Developer/CommandLineTools/usr/bin/ranlib
-- DESKTOP :
-- PATH : /Users/JP/.jenv/shims:/usr/local/mysql/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin
-- Configuring done
CMake Warning (dev):
Policy CMP0042 is not set: MACOSX_RPATH is enabled by default. Run "cmake
--help-policy CMP0042" for policy details. Use the cmake_policy command to
set the policy and suppress this warning.
MACOSX_RPATH is not specified for the following targets:
calculMean
This warning is for project developers. Use -Wno-dev to suppress it.
-- Generating done
-- Build files have been written to: /Users/JP/Desktop/ChimericGenomeMaker/build
Scanning dependencies of target calculSum
[ 20%] Building CXX object CMakeFiles/calculSum.dir/src/static/calc_sum.cpp.o
Linking CXX static library libcalculSum.a
[ 20%] Built target calculSum
Scanning dependencies of target ChimericGenomeMaker
[ 40%] Building CXX object CMakeFiles/ChimericGenomeMaker.dir/src/shared/calc_mean.cpp.o
[ 60%] Building CXX object CMakeFiles/ChimericGenomeMaker.dir/src/main/foo.cpp.o
[ 80%] Building CXX object CMakeFiles/ChimericGenomeMaker.dir/src/main/main.cpp.o
Linking CXX executable ChimericGenomeMaker
[ 80%] Built target ChimericGenomeMaker
Scanning dependencies of target calculMean
[100%] Building CXX object CMakeFiles/calculMean.dir/src/shared/calc_mean.cpp.o
Linking CXX shared library libcalculMean.dylib
Post-build is Running
POST-BUILD SCRIPT
HELLO YOU!
[100%] Built target calculMean
[ 20%] Built target calculSum
[ 80%] Built target ChimericGenomeMaker
[100%] Built target calculMean
Install the project...
-- Install configuration: ""
-- Installing: /Users/JP/Desktop/ChimericGenomeMaker/build/install/lib/shared/libcalculMean.dylib
-- Installing: /Users/JP/Desktop/ChimericGenomeMaker/build/install/lib/static/libcalculSum.a
-- Installing: /Users/JP/Desktop/ChimericGenomeMaker/build/install/bin/ChimericGenomeMaker
m1-p19-gen:ChimericGenomeMaker JP$
【问题讨论】:
-
它根本与 C++ 无关。您需要了解动态链接器在加载可执行文件时如何定位动态库。但是我对 CMake 一无所知,因此无法提供进一步的帮助。
标签: compiler-construction linker cmake shared-libraries static-libraries