简体   繁体   中英

Xcode/IOS: linking a CMake library

I recently tried to link a C++ "CMake" project to an existing IOS project, I was able to include headers into Xcode using Project > Build Setting > Header Search Path , and also added libxeuus.a into build phase, but suddenly when I wanted to use method inside my lib Xcode raise an error with description of :

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)

but I checked my library architecture with lipo it seems to be okay.

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

Here is my main.mm file:

#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]));
    }
}

here is my headers/xeuus.h

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

here is my headers/test.h

#ifndef test_h
#define test_h

#include <iostream>
#include <string>

int hello();

#endif

and here is 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;
}

and 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)

I tried every solution on the internet non of them working for me, also I tried Shared library, but that doesn't work either.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM