简体   繁体   中英

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

I'm trying to make a C++ wrapper for Unity3D in C# based in OpenCV but first of all I'm trying to make a .a static library in C++ in order to make the wrapper. (I don't know if I'm doing right this).

But my problem is on building a new static library with Xcode.

(PD: I'm asking this question because Google is full of "how to make a static library for iOS and that's not what I'm looking for).

I make: New -> Project -> Mac OSx Frameworks and Libraries -> Library (Static, STL framework).

Then I have my two files: .h and .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

I press Cmd + B to build the library and I try to make a new Command Line project (for testing), importing my libfile1.a and my header file1.h in the Command Line project:

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;
}

but I get linker errors (it's like if my static library was compiled in a wrong way):

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)

Do you know what I'm really doing bad?

Thank you in advance. Regards.

In file1.cpp , instead of

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

write

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

Otherwise you're not defining a member function of file1 but a free function that just happens to have the same name.

Oh, and you'll have to #include "file1.h" in file1.cpp to make the class definition known there.

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