Author Topic: GCC/G++ - how to export global variable without underscore?  (Read 682 times)

Andi B.

  • Hero Member
  • *****
  • Posts: 842
  • Karma: +11/-2
    • View Profile
I'm trying to build a dll which should export some functions and global variables. I made this long time ago I think with our gcc3.xx but now want to do the same with our g++.exe (GCC) 9.2.0.

I think exporting symbols is disabled now by default so I added to the functions I want to export
Code: [Select]
__attribute__ ((dllexport)) which works fine. Now when I look at my dll with
Code: [Select]
lxlite /C:exp myown.dllI see the exported functions as I expected.

But my global variables still have the C naming convention leading _ which I don't like. F.i. I have -
Code: [Select]
00048 │ _NdpAttribute
00049 │ _NdpPropertiesInfo
00050 ┴ _NdpTypes
but I wanna have them without the _

The variables are defined that way -
Code: [Select]
__attribute__ ((dllexport)) extern const char *NdpTypes[];and I use g++ instead gcc. So I thought it should work the same as with the functions (they don't have the _).

Any thoughts?


Dave Yeo

  • Hero Member
  • *****
  • Posts: 4874
  • Karma: +103/-1
    • View Profile
Re: GCC/G++ - how to export global variable without underscore?
« Reply #1 on: June 30, 2024, 11:13:08 pm »
It's the calling convention, cdecl vs stdcall. I think you need __attribute__ ((__cdecl)), might be ((__stdlib)) and __declspec instead of __attribute__
Code: [Select]
__attribute__ ((dllexport)) __attribute__((__stdcall)) extern const char *NdpTypes[];
or __cdecl
Been a while since I thought about this so sorry for being unclear, needed it in the past for linking OW and GCC.
gcc vs g++ shouldn't matter

Andi B.

  • Hero Member
  • *****
  • Posts: 842
  • Karma: +11/-2
    • View Profile
Re: GCC/G++ - how to export global variable without underscore?
« Reply #2 on: July 15, 2024, 09:04:07 am »
I tried various combinations of dllexport (which seems to be mandatory to export the symbol) and stdcall cdecl... But I ever end up with exporting _NdpTypes instead NdpTypes. I was under the impression that c++ don't add the _ but obviously I misinterpreted some stuff.

Another idea, is there any emx tool which can modify the exported symbols? Or add a new symbol to a dll which references to the orignal?

Btw. this all started cause I didn't manage to convince c++/linker to work with a .def file as it was used with icc or watcom. You know I'm not experienced with all this stuff so there is much guessing on my side.
« Last Edit: July 15, 2024, 09:11:45 am by Andi B. »

Paul Smedley

  • Hero Member
  • *****
  • Posts: 2139
  • Karma: +166/-0
    • View Profile
Re: GCC/G++ - how to export global variable without underscore?
« Reply #3 on: July 15, 2024, 10:16:44 am »
Isn't it _System to use non-underscored variable names?

Dave Yeo

  • Hero Member
  • *****
  • Posts: 4874
  • Karma: +103/-1
    • View Profile
Re: GCC/G++ - how to export global variable without underscore?
« Reply #4 on: July 15, 2024, 06:02:14 pm »
Isn't it _System to use non-underscored variable names?

Might be.

Dave Yeo

  • Hero Member
  • *****
  • Posts: 4874
  • Karma: +103/-1
    • View Profile
Re: GCC/G++ - how to export global variable without underscore?
« Reply #5 on: July 15, 2024, 06:16:37 pm »
I tried various combinations of dllexport (which seems to be mandatory to export the symbol) and stdcall cdecl... But I ever end up with exporting _NdpTypes instead NdpTypes. I was under the impression that c++ don't add the _ but obviously I misinterpreted some stuff.

Another idea, is there any emx tool which can modify the exported symbols? Or add a new symbol to a dll which references to the orignal?

Btw. this all started cause I didn't manage to convince c++/linker to work with a .def file as it was used with icc or watcom. You know I'm not experienced with all this stuff so there is much guessing on my side.

You only need dllexport if you don't have the symbols in the def file. Traditionally, before we got dllexport, you hand wrote a DEF file including the exports, either by name or ordinal or you ran emxexp on the
object files and piped the results to the def file. Run emxexp with no parameters to see usage..
I've only done it the other way, pulling in symbols from a GCC DLL to a OW binary.
Note that with OW, you have to be careful to use the right calling convention to begin with, it defaults to its own calling convention which is incompatible with GCC DLLs, it uses registers instead of the stack.
See "Open Watcom 2.0 C/C++ Programmers Guide under Commonly Asked Questions, Converting to OpenWatcom C/C++, Conversion from IBM-compatible PC compilers.
In the case of porting from icc or OW, I simply edit the DEF, adding the underscores

Edit: You might also need the extern keyword
« Last Edit: July 15, 2024, 06:18:25 pm by Dave Yeo »