2014-06-06 75 views
0

我有一個在Mac上開發的C++項目。我可以通過命令行與忍者或gmake建立它沒有問題,但一旦我生成的Xcode項目失敗。CMake生成的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) 
+0

說它無法找到.a文件。我顯然可以將它拖入Xcode,但我寧願讓CMake爲我做。 – ruipacheco

+0

是的,我有CMake中的說明來生成文件。它是示例代碼中由STATIC命令生成的。 – ruipacheco

回答

3

您不能使用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>. 
+0

它在那裏,我正在構建一個可執行文件。 – ruipacheco

+0

我很抱歉,但我的代碼似乎與您的示例沒有任何區別。唯一的區別是我在創建可執行源文件的路徑時展開CMake變量。 – ruipacheco

+0

這同樣適用於庫對象。 – ruipacheco