【问题标题】:Xcode/IOS: linking a CMake libraryXcode/IOS:链接 CMake 库
【发布时间】:2018-04-08 07:38:38
【问题描述】:

我最近尝试将 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)

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

【问题讨论】:

    标签: c++ ios xcode cmake linker


    【解决方案1】:

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

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

    【讨论】:

    • 我在定义 iOS 平台后收到“不支持的架构”错误。您对如何解决这个问题有任何想法吗? (我正在尝试构建 CLucene。)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-20
    • 2015-11-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2013-01-06
    • 1970-01-01
    相关资源
    最近更新 更多