4

我有一个简单的 cmake 项目,我无法在 OS X 10.8.4 上编译。cmake/make 过程在 Linux 上运行良好,但在 OS XI 上出现此错误:

Linking CXX static library libImageFilter.a
ar: no archive members specified
...
make[2]: *** [lib/libImageFilter.a] Error 1
make[1]: *** [lib/CMakeFiles/ImageFilter.dir/all] Error 2
make: *** [all] Error 2

我在两个平台上都使用 Eclipse CDT4 Generator Unix MakeFile。这似乎与两个系统上的 ar 之间的区别有关。但是,我在谷歌上找不到太多可以帮助我解决问题的方法。

这里有更多信息给你

src/CMakeList.txt

make_minimum_required(VERSION 2.8)
project(itkNormals)
FIND_PACKAGE (ITK REQUIRED)
IF( ITK_FOUND )
  include( ${ITK_USE_FILE} )
ENDIF( ITK_FOUND )
add_subdirectory(test)
add_subdirectory(lib)

src/lib/CMakeList.txt

add_library(DotImageFilter itkDotImageFilter.h)
SET_TARGET_PROPERTIES(DotImageFilter PROPERTIES LINKER_LANGUAGE CXX)
target_link_libraries(DotImageFilter ${ITK_LIBRARIES})

add_library(ImageFilter itkImageFilter.hxx)
SET_TARGET_PROPERTIES(ImageFilter PROPERTIES LINKER_LANGUAGE CXX)
target_link_libraries(ImageFilter ${ITK_LIBRARIES})

src/test/CMakeLists.txt:

include_directories(${PROJECT_SOURCE_DIR}/lib)

add_executable(itkNormalsMain itkNormals.cxx)
TARGET_LINK_LIBRARIES(itkNormalsMain ${ITK_LIBRARIES})
TARGET_LINK_LIBRARIES(itkNormalsMain ImageFilter)
TARGET_LINK_LIBRARIES(itkNormalsMain DotImageFilter)

add_executable(dotTestMain dotTester.cxx)
TARGET_LINK_LIBRARIES(dotTestMain ${ITK_LIBRARIES})
TARGET_LINK_LIBRARIES(dotTestMain ImageFilter)
TARGET_LINK_LIBRARIES(dotTestMain DotImageFilter)

add_executable(IST ImageSourceTest.cxx)
TARGET_LINK_LIBRARIES(IST ${ITK_LIBRARIES})
TARGET_LINK_LIBRARIES(IST ImageFilter)
4

1 回答 1

7

You can't create library from one header file:

add_library(ImageFilter itkImageFilter.hxx)
SET_TARGET_PROPERTIES(ImageFilter PROPERTIES LINKER_LANGUAGE CXX)
target_link_libraries(ImageFilter ${ITK_LIBRARIES})

that's the reason why you set LINKER_LANGUAGE explicitly - there is nothing to link and cmake is confused.

So include_directories is enough:

include_directories(${PROJECT_SOURCE_DIR}/lib)

BTW:

You don't need to check ITK_FOUND if you specify REQUIRED:

FIND_PACKAGE (ITK REQUIRED)
IF( ITK_FOUND )
  include( ${ITK_USE_FILE} )
ENDIF( ITK_FOUND )

from documentation:

The REQUIRED option stops processing with an error message if the package cannot be found.


PROJECT_SOURCE_DIR is not necessary equal itkNormals_SOURCE_DIR (you may use this file from other project):

include_directories(${PROJECT_SOURCE_DIR}/lib)

Can be fixed one of this way:

include_directories(${itkNormals_SOURCE_DIR}/lib)
include_directories(${CMAKE_CURRENT_LIST_DIR}/../lib)

or simply include from parent file:

# src/CMakeLists.txt
include_directories("./lib")
于 2013-11-14T06:48:14.023 回答