0

let's assume I do simple function designed to be exported as DLL

#include <iostream>
__declspec(dllexport) std::string __cdecl Hello(){
    return std::string("Hello world");
}

This dll is intended to be used with a program already using iostream so is it possible to do the same thing but don't include iostream ? (Maybe it's a stupid question)

If yes, how can I specify it to my compiler? (Mingw)

Xemuth
  • 415
  • 3
  • 12
  • `std::string` and `extern "C"` don't match up! – Ajay Aug 05 '19 at 08:50
  • Indeed sorry, I just corrected it – Xemuth Aug 05 '19 at 08:52
  • 1
    "do the same thing but don't include iostream" -> `#include ` will work. More seriously, do you want to remove `#include` from your DLL's header file or source file? The latter does not make sense (code will not compile), but you can use forward declarations to get rid of majority of includes from header files (in order to keep client's code less polluted and compile faster). EDIT, as for `std` stuff, there is `#include` header, https://stackoverflow.com/questions/10289766/forward-declaration-of-variables-classes-in-std-namespace – R2RT Aug 05 '19 at 09:00

2 Answers2

2

Each DLL would need to pass preprocessing, compilation and linking phase. DLL is the same as any other type of project (except static library). Hence, it needs all #includes, library files, and all symbols fully resolved.

If DLL need to use iostream, or any STL class for that matter - the respective code must #include appropriate header.

Ajay
  • 18,086
  • 12
  • 59
  • 105
1

Each source file that uses a standard library facility needs a corresponding #include directive. It doesn't matter whether the file is built into a DLL or not.

Your file does not use any iostreams facilities, but it does use strings, so #include <string> is mandatory. If you omit #include <string> but include some other standard header, it may or may not work.

Ajay
  • 18,086
  • 12
  • 59
  • 105
n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243