在 C++ 中创建静态库 (.a)(不适用于 iOS)

Creating a static library (.a) in C++ (Not for iOS)

我试图在基于 OpenCV 的 C# 中为 Unity3D 制作一个 C++ 包装器,但首先我试图在 C++ 中制作一个 .a 静态库以制作包装器。 (我不知道我这样做对不对)。

但我的问题是用 Xcode 构建一个新的静态库。

(PD:我问这个问题是因为 Google 充满了“如何为 iOS 制作静态库,而这不是我要找的东西)。

我制作:新建 -> 项目 -> Mac OSx 框架和库 -> 库(静态,STL 框架)。

然后我有两个文件:.h.cp

file1.cp:

#include <iostream>
#include "/Users/rafaelruizmunoz/Documents/OpenCV_projects/OpenCVDebug/OpenCVDebug/mylib.h"

using namespace std;
using namespace cv;

float* prueba_funcion(unsigned char* a, int cols, int rows) {
    Mat mat = Mat(rows, cols, CV_8U, a);
    float* o = giveMeMiddlePoints(mat); // this function gets called from the include
    return o;

}

file1.h

#ifndef file1_
#define file1_

/* The classes below are exported */
#pragma GCC visibility push(default)

class file1
{
    public:
        float* prueba_funcion(unsigned char *, int, int);
};

#pragma GCC visibility pop
#endif

我按 Cmd + B 构建库并尝试创建一个新的命令行项目(用于测试),导入我的 libfile1.a 和我的 header file1.h 在命令行项目中:

main.cpp

#include <iostream>
#include "/Users/rafaelruizmunoz/Desktop/file1.h"

using namespace std;

int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";

    unsigned char data[9] = {3,3,3,1,9,1,2,2,2};

    file1 f;
    float* q = f.prueba_funcion(data, 3, 3);
    cout << endl << q;

    return 0;
}

但我收到链接器错误(就像我的静态库以错误的方式编译):

Undefined symbols for architecture x86_64: "file1::prueba_funcion(unsigned char*, int, int)", 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)

你知道我做的不好吗?

提前谢谢你。 问候。

file1.cpp,而不是

float* prueba_funcion(unsigned char* a, int cols, int rows)

float* file1::prueba_funcion(unsigned char* a, int cols, int rows)

否则你不是在定义 file1 的成员函数,而是一个恰好同名的自由函数。

哦,您必须在 file1.cpp#include "file1.h" 才能使 class 定义在那里已知。