Author Topic: DLL Exported function list - is too exhaustive - things I don't want in there!  (Read 9131 times)

Offline adder6

  • Single posting newcomer
  • *
  • Posts: 8
Hi!

Quick one (I hope!). I'm using a DLL file all is working fine but I really want to do it using a .DEF file. At the moment even if I include a .DEF file in the project specifying say only two functions, CB exports absolutely everything in the DLL including some global variables and class member functions by the looks.

I don't want this. I only want it to consult the .DEF file and then export only the functions I want it to export. I'm sure there is or will be a setting in the project options which can force this behaviour, but I've no idea where it is?

I searched for DLL stuff but mostly it was asking about how to get DLL's to work, not how to achieve this particular aim.

I'd appreciate any help, anyone can offer - thanks :D

**Note I don't even need to have the .DEF file included in the project for it to export all of this stuff either. CB seems to be doing it's own thing at the moment.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Wrong forum mate...
Read this: http://wiki.codeblocks.org/index.php?title=FAQ#Q:_What_Code::Blocks_is_not.3F

BTW, there was a topic about the same problem, not long ago...
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline adder6

  • Single posting newcomer
  • *
  • Posts: 8
Thanks for the reply. I've tried searching again to not much avail, some of the topic titles are too short and not descriptive enough perhaps.

It seems this is more a linker/compiler issue perhaps. I assume proper research into the stuff CB leverages might reveal how to do this. I assume some of those blank option fields in the project settings menus are commands you can give to the compiler/linker?

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7588
    • My Best Post
http://wiki.codeblocks.org/index.php?title=FAQ#Q:_How_do_I_troubleshoot_an_compiler_problem.3F

Learn the name of your Compiler and OS; post the name of your Compiler and OS.

Post compiler questions on a site the supports your compiler; this is NOT a compiler support site.

If you figure out the right command to send to YOUR compiler; ask on this forum how to add the commands to Code::Blocks.
Or, if you find a command Code::Blocks is sending to the compiler that you think is should not; ask how to remove the command.

Tim S.
« Last Edit: March 21, 2011, 01:21:58 pm by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline adder6

  • Single posting newcomer
  • *
  • Posts: 8
Thanks for the reply. Makes much sense. Here is the type of C++ compiler and dynamic library compiler I am using:

mingw32-g++.exe

I noticed there are places in the project options for build options. Seems you can pass specific instructions to the compiler from here. I believe CB is currently setup to request all available functions and global data items to be subject to exportation. If possible I would like to know what special build options to add to only make it export those found in the .DEF file. If this is even possible. Maybe someone on here will know, but I think first I will ask on mingw forum site.

At the moment it looks like standard compiler build option for DLL files uses following type of commands:

g++ -c -DBUILDING_EXAMPLE_DLL example_dll.cpp
g++ -shared -o example_dll.dll example_dll.o -Wl,--out-implib,libexample_dll.a

for a file/project named example_dll. -DBUILDING part of the commands I believe causes all found functions to be exported using dllexport. This is probably what my problem is.

I will ask on mingw forum for more information then reply here again. Thanks  :D

Offline adder6

  • Single posting newcomer
  • *
  • Posts: 8
Out of interest here are the commands CB is issuing to the compiler/linker (I can never tell which it is).

Code
mingw32-g++.exe -shared -Wl,--output-def=bin\Debug\libtest.dll.def -Wl,--out-implib=bin\Debug\libtest.dll.a -Wl,--dll  obj\Debug\example_dll.o   -o bin\Debug\test.dll

I'm really not sure which part is responsible for causing all contained items in the object file to be exported. I'll do some more research and return. I couldn't interestingly find the command --dll in the GCC documentation. Maybe I'm looking at an incorrect version of the manual. Anyways I'll reply if I can find anymore useful information.

** I also noticed this command here:

Code
mingw32-g++.exe  -Wall -DBUILD_DLL -g

But even when compiling with that BUILD_DLL flag removed it still does it. I think maybe now it is best to avoid worrying about this and just accepting it. I wouldn't think that given the C++ name mangling any of those unwanted exported functions would ever be called anyway  :D

« Last Edit: March 21, 2011, 04:27:36 pm by adder6 »

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7588
    • My Best Post
Code
-Wl,
Hint: The above means send the option to the linker.
(Binutils linker might be which linker it means; I really can not remember which linker it goes to.)

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
ld (gnu default linker) or gold (also named ld)...
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline adder6

  • Single posting newcomer
  • *
  • Posts: 8
Thanks for the repliec chaps but I think it's probably time to move on anyway. It's not a critical problem just something that would have been better without. I may yet return to it but after studying GCC manual and looking at the complete full command line output it seems the compiler is paying no attention whatsoever to the .DEF file attached to the project. Even if those Wl options can be changed I still believe this will just export everything no matter what, and nothing CB can do about that seems to be the way the linker & compiler are setup.

Thanks anyway :D

Offline adder6

  • Single posting newcomer
  • *
  • Posts: 8
I have now solved this problem, at least it appears that way for now. If anyone is interested or you think it might be useful please say so on this topic and I will post steps showing exactly how I did it. Many thanks :D

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
I have now solved this problem, at least it appears that way for now. If anyone is interested or you think it might be useful please say so on this topic and I will post steps showing exactly how I did it. Many thanks :D
...sure.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline adder6

  • Single posting newcomer
  • *
  • Posts: 8
Ok I'll be honest this is rough as hell at the moment although perfectly functional. But to anyone who knows a bit about this or who doesn't know anything about it but has spent several days searching trying all kinds of solutions to the problem - this should be child's play.

1)I wrote a simple .cpp file containing two functions. One name Double and one named Treble. Both take a plain good old int as an argument and return it either doubled or trebled depending on which function you call. The file has no header attached:

Code
// file example.cpp
#include <stdio.h> // this include may be unnecessary - probably forgot to remove it

extern "C"
{
    int Double(int x)
        {
            return 2 * x;
        }
}

int Treble(int x)
{
    return 3 * x;
}

2)I compiled and built the .cpp file to make a typical object .o file from example.cpp.

3)I made a standard .DEF file contents as follows:

Code
;test.def
;The following functions are exported from this DLL
LIBRARY   "test.dll"
EXPORTS
   Double   @1

File's name is test.def.

4)I navigated to the installation folder of CB and then navigated to the 'MinGW' folder and then finally the 'bin' folder. This seems to stop those nasty No Such Directory Errors - for some reason doing stuff here allows g++ to get access to a load of stuff it needs to run - don't ask me why. I place both the 'test.def' file and the 'example.o' file in this 'bin' folder.

5)Opening the command line I navigated to the same place (the 'bin' folder in the CB 'MinGW' folder).

6)I then inputted the following command (I'm fairly sure this is correct may need to check it):

Code
g++ -shared -Wl,--output-def=newtest.def -o newtest.dll test.def example.o

The program runs, reads the original .def file ('test.def') and outputs a new .def file ('newtest'.def) and a new dll ('newtest.dll') which has only the exports stated in the original 'test.def' file exported. I check it when I opened up newtest.def it had this:

Code
LIBRARY "test.dll" BASE=0x67780000
EXPORTS
    Double @1

Great! When I ran a program that linked against this it was fine, and if I tried to access function 'Treble' it borked as it should because it wasn't requested to be exported. Normally when just building through CB the output .def file looked like this:

Code
EXPORTS
    Double @1
    _Z6Treblei @2

With that damn unwanted name mangled Treble function. So I guess maybe that's it. Obviously now you/I would/will have to build on this to get dependency files into the commands and stuff but that's a fairly lengthy but obvious process which can be done by just checking the full command line output from CB and working out how it's done.

I hope that's useful ;)

**Note in the .def files I state the name of the library is 'test.dll' but I asked the program to output 'newtest.dll' as the output. This might screw something up when linking against it but again should be easy to fix probably just need to change the command line instruction to output 'test.dll' not 'newtest.dll'. It's only rough at the moment anyway but I'm sure you get the idea  :wink:
« Last Edit: March 22, 2011, 09:35:25 pm by adder6 »

Offline adder6

  • Single posting newcomer
  • *
  • Posts: 8
I'm a bit embarassed here but I think this might all simply be possible by just typing the full filename of your def file in the linker options in the project build options.

By right clicking on the project and going into project build options and going to the linker settings tab, you can then go to the 'Other linker options' field and just type in your .def file's name and then compile and build. So far this seems to have exactly the same effect as all those lengthy steps I posted above :?