Is it possible to add extra classes to a dll?

Asked

Viewed 61 times

1

The compiler can even compile perfectly , but for some reason when I run the app.exe gives me an error, 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 on 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 add a class outside the common header violates the link.

  • Add to the question the error you make when you run app.exe

  • 2

    I copied and executed your code here and it worked well, I did on Linux direct. I only had to take the -static of your compiler command.

  • it seems that mingw requires libstdc++ to compile statically tbm in dll

  • For test purposes, compile the file cpp for .obj and then put to compile together the main, works?

  • no, of the same thing

2 answers

1

In the latest updates Mingw connects to the standard c++ library dynamically by default. In this case the generated dll is also dynamically dependent on this library, it seems 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 it was enough to change the compilation code to :

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

The mistake wouldn’t happen if the ClassB was removed because I also removed operator new , which removed the stdc library dependency++ !!!

0

The code structure is not the right one, you should have a cpp file (c plus plus/c++) and an h file (header) for each class. Your problem (without seeing the error) would say that this is circular inclusion, in addition to the structure.
The file. h server to create the class definition, while the cpp file serves to implement the class logic (class only!!). So I’d say 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;
}

Classeb also needs to be in other files.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.