CMAKE没有规则为内部共享库制作目标

时间:2015-10-19 08:17:13

标签: c cmake shared-libraries libraries

我写了一个cmake文件,它构建了两个共享库,一个vector3.c和一个matrix4.c matrix4依赖于vector3,如果我运行我的cmake项目并且只构建它建立的vector3共享库就好了,当我添加matrix4共享库时,我得到一个cmake错误。没有规则来制定目标。

文件夹结构

core_math
 ------CMakeLists.txt
-common/
 ------common_structs.h
-matrix4/
 ------CMakeLists.txt
 ------matrix4/src/matrix4_scalar.c
 ------matrix4/headers/matrix4_scalar.h
-vector3/
-------CMakeLists.txt
-------vector3/src/vector3_scalar.c
-------vector3/headers/vector3_scalar.h

core_math /的CMakeLists.txt

PROJECT(core_math)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

ADD_SUBDIRECTORY(vector3_scalar)
ADD_SUBDIRECTORY(matrix4_scalar)
SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY $(CMAKE_BINARY_DIR)/static_lib)
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY $(CMAKE_BINARY_DIR)/shared_lib)
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY $(CMAKE_BINARY_DIR)/bin)

SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake_modules)

core_math /的Vector3 /的CMakeLists.txt

PROJECT(vector3_scalar)
SET(CMAKE_MACOSX_RPATH 1)

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../common)

SET(HEADER_FILES ${CMAKE_CURRENT_SOURCE_DIR}/../common/common_structs.h) 
SET(SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/vector3_scalar.c ${HEADER_FILES})

ADD_LIBRARY(vector3_scalar SHARED ${SRC_FILES})

core_math / matrix4_scalar /的CMakeLists.txt

PROJECT(matrix4_scalar)
SET(CMAKE_MACOSX_RPATH 1)

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../common 
    ${CMAKE_CURRENT_SOURCE_DIR}/../vector3_scalar)


SET(HEADER_FILES ${CMAKE_CURRENT_SOURCE_DIR}/../common/common_structs.h
    ${CMAKE_CURRENT_SOURCE_DIR}/../vector3_scalar/headers/vector3_scalar.h
    ${CMAKE_CURRENT_SOURCE_DIR}/headers/matrix4_scalar.h)
SET(SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/matrix4_scalar.c ${HEADER_FILES})

ADD_LIBRARY(matrix4_scalar SHARED ${SRC_FILES})

TARGET_LINK_LIBRARIES(matrix4_scalar vector3_scalar)

构建错误

-- Build files have been written to: /Users/blubee/MATH/project/build
Scanning dependencies of target vector3_scalar
[ 25%] Building C object source/core_math/vector3_scalar/CMakeFiles/vector3_scalar.dir/sr
c/vector3_scalar.c.o
[ 50%] Linking C shared library $(CMAKE_BINARY_DIR)/lib/libvector3_scalar.dylib
[ 50%] Built target vector3_scalar
Scanning dependencies of target matrix4_scalar
[ 75%] Building C object source/core_math/matrix4_scalar/CMakeFiles/matrix4_scalar.dir/sr
c/matrix4_scalar.c.o
make[2]: *** No rule to make target `source/core_math/vector3_scalar//Users/blubee/MATH/pr
oject/build/lib/libvector3_scalar.dylib', needed by `source/core_math/matrix4_scalar//Use
rs/blubee/MATH/project/build/lib/libmatrix4_scalar.dylib'.  Stop.
make[1]: *** [source/core_math/matrix4_scalar/CMakeFiles/matrix4_scalar.dir/all] Error 2
make: *** [all] Error 2

如果我从matrix4_scalar cmakelists.txt文件中删除了target_link_libraries,那么我会得到未定义的符号错误。

Undefined symbols for architecture x86_64:
  "_vec3_deg2rad", referenced from:
      _mat4_rotate in matrix4_scalar.c.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [source/core_math/matrix4_scalar//Users/blubee/MATH/project/build/lib/libmatr
ix4_scalar.dylib] Error 1
make[1]: *** [source/core_math/matrix4_scalar/CMakeFiles/matrix4_scalar.dir/all] Error 2
make: *** [all] Error 2

1 个答案:

答案 0 :(得分:4)

我已经对您的代码进行了一些测试,可以重现问题。

这与在$(...)命令中使用SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ...)括号进行变量解除引用有关。我已将$(CMAKE_BINARY_DIR)更改为${CMAKE_BINARY_DIR},一切正常:

> cmake --build .
-- Configuring done
-- Generating done
-- Build files have been written to: build
Scanning dependencies of target vector3_scalar
[ 25%] Building C object vector3_scalar/CMakeFiles/vector3_scalar.dir/src/vector3_scalar.c.o
[ 50%] Linking C shared library ../shared_lib/libvector3_scalar.dylib
[ 50%] Built target vector3_scalar
Scanning dependencies of target matrix4_scalar
[ 75%] Building C object matrix4_scalar/CMakeFiles/matrix4_scalar.dir/src/matrix4_scalar.c.o
[100%] Linking C shared library ../shared_lib/libmatrix4_scalar.dylib
[100%] Built target matrix4_scalar

<强>参考