Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
266 views
in Technique[技术] by (71.8m points)

c++ - header file for exporting class from shared library

I have built a shared lib (.so) that contains several classes. I now need to create the header file to use to import those classes into another app. Some of the exported classes have private data/functions. Do those need to be included in that header file?

eg:

orig.h (header used to build the lib)

#ifndef __MYLIB__
#define __MYLIB__

// system includes here

#ifdef __cplusplus
extern "C" {
#endif

namespace myspace{
// my includes
#include "..."

#ifndef EXPORT_API
#define EXPORT_API   __attribute__((__visability__("default")))
#endif

class EXPORT_API MyClass1 {
public:
   MyClass1();
   virtual ~MyClass1();

public:
    ...        // public members 

protected:
   ...        // protected members

private:     // private members
   ...       // these should not be visible outside of the lib


};
} //namespace
#ifdef __cplusplus
}
#endif
#endif  // __MYLIB__

This builds okay.

exp.h used by users of the lib to build their app

#ifndef __MYLIB__
#define __MYLIB__

// system includes here

#ifdef __cplusplus
extern "C" {
#endif

namespace myspace{

class  MyClass1 {
public:
   MyClass1();
   virtual ~MyClass1();

public:
    ...        // public members 

protected:
   ...        // protected members

// no private members listed here.

};
} //namespace
#ifdef __cplusplus
}
#endif
#endif //__MYLIB__

In my userapp.h:

#ifndef __MYUSER__
#endif
#include <...> //system includes

#ifdef __cplusplus
extern "C" {
#endif

#include "exp.h"

//derived class
class MyClass2 : public MyClass1 {
public:
   MyClass2();
   ~virtual MyClass2();

public:
  // derived class members
  myclass2_func();

  int   m_myclass2int;
};

In main.cpp: #include "userapp.h"

using myspace;
int main(int *argc, char** argv){

MyClass2   theClass;

// make member calls to theClass
theClass.myclass2_func();
theClass.m_myclass2int = 0;

return 0;
}

This also builds okay.

But when I debug userapp, memory gets corrupted, most likely from bad pointers. This is typically when I step through derived class functions (eg. myclass2_func), either I get a segmentation fault (memory access violation) or sometimes gdb itself crashes (when trying to display values). I am guessing that this has to do with the private section, inside the lib, that I didn't specify in exp.h.

So back to the original question: do those private sections need to be included in the import header file?

TIA

ken

question from:https://stackoverflow.com/questions/65927753/header-file-for-exporting-class-from-shared-library

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...