CMake生成的Xcode项目无法编译

时间:2014-06-06 09:47:29

标签: c++ xcode cmake static-libraries googletest

我有一个我在Mac上开发的C ++项目。我可以通过命令行使用ninja或gmake构建它而没有问题但是一旦我生成了Xcode项目就失败了。

项目本身是一个静态库,链接到运行googletest的命令行应用程序。

我得到的错误是,当构建最终的可执行文件时,Xcode说它无法找到静态库。这是我的CMakeLists.txt:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

PROJECT(MyProject)

SET(CMAKE_VERBOSE_MAKEFILE 0)  # set to 1 for verbose Makefile

SET(MyProject_VERSION_MAJOR 1)
SET(MyProject_VERSION_MINOR 0)
SET(CMAKE_BUILD_TYPE DEBUG)

ENABLE_LANGUAGE(CXX)

# Build the static lib
FILE(GLOB SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
FILE(GLOB G2LOG "${CMAKE_CURRENT_SOURCE_DIR}/src/g2log/*.cpp")
FILE(GLOB LIBZIP "${CMAKE_CURRENT_SOURCE_DIR}/src/libzip/*.c")
FILE(GLOB LIBXML "${CMAKE_CURRENT_SOURCE_DIR}/src/libxml/*.c")
FILE(GLOB LIBXSLT "${CMAKE_CURRENT_SOURCE_DIR}/src/libxslt/*.c")

# Settings that work with Unix, Linux and OSX
IF (UNIX)
    # Ideally we should use -Werror but libxml2 will break
    SET(FLAGS "-Wno-attributes -g -Wall -std=c++11 -stdlib=libc++ -Wunused -D_GLIBCXX_USE_NANOSLEEP -m64")
    SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_REENTRANT -DTRIO_HAVE_CONFIG_H")
    FILE(GLOB to_remove "${CMAKE_CURRENT_SOURCE_DIR}/src/g2log/crashhandler_win.cpp")

    IF (${CMAKE_BUILD_TYPE} MATCHES DEBUG)
        SET(FLAGS "${FLAGS} -DGOOGLETEST")
    ENDIF(${CMAKE_BUILD_TYPE} MATCHES DEBUG)
ENDIF(UNIX)

IF (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
    SET(CMAKE_CXX_FLAGS  "${FLAGS}")
    INCLUDE_DIRECTORIES("/System/Library/Frameworks/")
    FIND_LIBRARY(APP_SERVICES_LIBRARY ApplicationServices)
ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")

IF (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
    SET(CMAKE_CXX_FLAGS  "${FLAGS} -rdynamic")
ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Linux")

IF (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
    FILE(GLOB to_remove "${CMAKE_CURRENT_SOURCE_DIR}/src/g2log/crashhandler_unix.cpp")
ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Windows")

LIST(REMOVE_ITEM G2LOG ${to_remove})

# Build the main library
ADD_LIBRARY(g2log OBJECT ${G2LOG})
ADD_LIBRARY(libzip OBJECT ${LIBZIP})
ADD_LIBRARY(core OBJECT ${SOURCES})
ADD_LIBRARY(libxml OBJECT ${LIBXML})
ADD_LIBRARY(libxslt OBJECT ${LIBXSLT})
ADD_LIBRARY(MyProject STATIC $<TARGET_OBJECTS:core> $<TARGET_OBJECTS:libxslt> $<TARGET_OBJECTS:libxml> $<TARGET_OBJECTS:libzip> $<TARGET_OBJECTS:g2log>)

ADD_EXECUTABLE(MyProjectTests "${CMAKE_CURRENT_SOURCE_DIR}/tests/src/MyProjectTests.cpp")
TARGET_LINK_LIBRARIES(MyProjectTests MyProject)
IF (UNIX)
    TARGET_LINK_LIBRARIES(MyProjectTests "${CMAKE_CURRENT_SOURCE_DIR}/src/somelib/libsomelib.a")
    TARGET_LINK_LIBRARIES(MyProjectTests "${APP_SERVICES_LIBRARY}")
    TARGET_LINK_LIBRARIES(MyProjectTests "${CMAKE_CURRENT_SOURCE_DIR}/tests/src/libgtest.a")

    FIND_PACKAGE(ZLIB REQUIRED)
    IF (ZLIB_FOUND)
        INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIRS})
        TARGET_LINK_LIBRARIES(MyProjectTests ${ZLIB_LIBRARIES})
    ENDIF(ZLIB_FOUND)

    IF (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
        # In OSX link to iconv installed by Macports
        LINK_DIRECTORIES("/opt/local/lib/")
        TARGET_LINK_LIBRARIES(MyProjectTests iconv)
    ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
ENDIF(UNIX)

1 个答案:

答案 0 :(得分:4)

您无法使用Xcode生成器创建仅包含目标文件的库。 来自documentation

Some native build systems may not like targets that have only object files,
so consider adding at least one real source file to any target that
references $<TARGET_OBJECTS:objlib>.