Xcode/IOS: linking a CMake library



我最近尝试将一个C++的"CMake"项目链接到现有的IOS项目,我能够使用Project > Build Setting > Header Search Path将标头包含在Xcode中,并将libxeuus.a添加到构建阶段,但是突然当我想在我的lib中使用方法Xcode时,会引发一个错误,描述为:

clang: warning: libstdc++ is deprecated; move to libc++ [-Wdeprecated] Undefined symbols for architecture x86_64:
"hello()", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

但我检查了我的库架构lipo它似乎还可以。

>> lipo -i libxeuus.a
input file libxeuus.a is not a fat file
Non-fat file: libxeuus.a is architecture: x86_64

这是我main.mm文件:

#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import <xeuus.h>
int main(int argc, char * argv[]) {
    hello();
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

这是我的headers/xeuus.h

#ifndef xeuus_h
#define xeuus_h
#include "test.h"
#endif

这是我的headers/test.h

#ifndef test_h
#define test_h
#include <iostream>
#include <string>
int hello();
#endif

这是sources/test.cpp

#include "test.h"
int hello() {
    auto x = 0;
    // check if c++11 working
    auto p = std::make_shared<int>(2);
    return 10;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.0)
set(PROJECT_NAME xeuus)
set(PROJECT_VERSION 1.0.1)
set(PROJECT_DESCRIPTION "A lib to be shared.")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_BUILD_TYPE Release)
set(XEUUS_HEADERS_DIR ./headers)
set(XEUUS_SOURCES_DIR ./sources)

include_directories(${XEUUS_HEADERS_DIR})
file(GLOB_RECURSE SOURCE_FILES
    ${XEUUS_SOURCES_DIR}/*.cpp
)
add_library(${PROJECT_NAME} STATIC ${SOURCE_FILES})
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17)
我尝试了

互联网上的所有解决方案,它们都不适合我,我也尝试了共享库,但这也不起作用。

通过在CMakeLists中定义IOS平台来修复.txt并使用CMake编译项目,然后将其添加到主项目中。

set (CMAKE_OSX_SYSROOT "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS11.2.sdk")

最新更新