Is it possible to add extra classes to a dll?

1

The compiler even compiles perfectly, but for some reason when I run app.exe it returns an error, it follows my code:

main.cpp

#include <iostream>
#include "cpp.h"

int main() {
  ClassA * a = new ClassA();
  std::cout << "Versao : " << a->version() << std::endl;
  delete a;
  return 0;
}

cpp.h

#ifndef HEADER
#define HEADER
class ClassA {
public:
  int val;
  ClassA ();
  int version();
};
#endif

cpp.cpp

#include "cpp.h"

class ClassB {
public:
  int val;
  ClassB() {
    val = 15;
  }
};

ClassA::ClassA() {
  ClassB * b = new ClassB();
  val = b->val;
  delete b;
}

int ClassA::version() {
  return val;
}

I compile in mingw-64 using the following commands:

g++ -shared -fpic cpp.cpp -o cpp.dll

g++ -static -static-libstdc++ -static-libgcc main.cpp cpp.dll -o app.exe

The app.exe when executed shows an error "The application could not be initialized correctly (0xc000007b)". If I remove ClassB , and put the value in val directly it works normally, which made me think that  adding a class outside the common header violates the link.

    
asked by anonymous 28.09.2017 / 22:22

2 answers

1

In the latest updates, MinGw connects to the default c ++ library dynamically by default. In this case the generated dll is also dynamically dependent on this library, it seems to me that the dll is trying to load it since the application does not show this error if "libgcc_s_seh-1.dll" is in the same directory.

To solve this, simply change the build code to:

g++ -static -static-libstdc++ -static-libgcc -shared -fpic cpp.cpp -o cpp.dll

The error did not happen if the ClassB was removed because I also removed the new operator, which removed the dependency of the stdc ++ library !!!

    
30.09.2017 / 13:59
0

The code structure is not the correct one, you should have a cpp file (c plus plus / c ++) and a file h (header) for each class. Your problem (without seeing the error) would say that it is circular inclusion, beyond the structure. The .h server file to create the class definition, while the cpp file serves to implement the class logic (class only !!). So I would say to create the file ClassA.cpp, ClassA.h, ClassB.cpp, ClassB.h and call the logic in the Main class.

ClassA.h

#ifndef CLASS_H
#define CLASS_H
class ClassA {
   public:
   int val;
   ClassA ();
   int version();
};
#endif


ClassA.cpp

ClassA::ClassA() {
   ClassB * b = new ClassB();
   val = b->val;
  delete b;
}

int ClassA::version() {
   return val;
}

ClassB also needs to be in other files.

    
29.09.2017 / 12:33