Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Remove leading underscores from functions in dll


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 14 - Last Post: Jun 8, 2016 5:47 AM Last Post By: acemary mary Threads: [ Previous | Next ]
Peter Van Hove

Posts: 48
Registered: 5/7/09
Remove leading underscores from functions in dll  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 1, 2015 4:08 PM
I'm building a dll from third party source to dynamically link in my code and because of the free nature of the project I need to see this working with free distribution bcc 5.5

All works, due to the work of the library author, but the functions are exported with an underscore and for the sake of conformity with other compilers we need to see the functions without underscores.

I have tried many switches for bcc32.exe
-xu
-Xu
-ux
-uX
-u-x-
-x-u
-vu
and some more combinations

Can anybody tell me what it should be exactly and if the switch actually works for bcc32 5.5 ?
david hoke

Posts: 616
Registered: 2/9/07
Re: Remove leading underscores from functions in dll  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 2, 2015 6:26 AM   in response to: Peter Van Hove in response to: Peter Van Hove
Don't know the direct answer to your question, but...

<Peter Van Hove> wrote in message news:707544 at forums dot embarcadero dot com...
I'm building a dll from third party source to dynamically link in my code
and because of the free nature of the project I need to see this working
with free distribution bcc 5.5

All works, due to the work of the library author, but the functions are
exported with an underscore and for the sake of conformity with other
compilers we need to see the functions without underscores.

I have tried many switches for bcc32.exe
-xu
-Xu
-ux
-uX
-u-x-
-x-u
-vu
and some more combinations

Can anybody tell me what it should be exactly and if the switch actually
works for bcc32 5.5 ?

Can you use impdef against the .dll as generated, and then link against the
generated .def file (which I think, but default, generates names without the
underscores)?
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Remove leading underscores from functions in dll  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 2, 2015 10:04 AM   in response to: david hoke in response to: david hoke
david wrote:

Can you use impdef against the .dll as generated, and then link
against the generated .def file (which I think, but default, generates
names without the underscores)?

No, it does not. It generates the same names that the DLL extually exports.
You have to edit the .def file manually to remove the underscores.

--
Remy Lebeau (TeamB)
Peter Van Hove

Posts: 48
Registered: 5/7/09
Re: Remove leading underscores from functions in dll  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 4, 2015 2:06 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
You have to edit the .def file manually to remove the underscores.

So switches won't work ?
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Remove leading underscores from functions in dll  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 5, 2015 12:01 PM   in response to: Peter Van Hove in response to: Peter Van Hove
Peter wrote:

So switches won't work ?

There are no BCC32 or IMPDEF switches to avoid the underscores. The use
of underscores in name-mangled exports is part of the __cdecl calling convention,
it cannot be disabled, only hidden with a .DEF file.

--
Remy Lebeau (TeamB)
Peter Van Hove

Posts: 48
Registered: 5/7/09
Re: Remove leading underscores from functions in dll  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 5, 2015 12:45 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:

it cannot be disabled, only hidden with a .DEF file.


I see. During the building process, as far as I can see, no *.DEF files are created.
Is this something I need to add and how do I go about this ?

What happens is that I run a script that creates Makefile.bcc files from existing source code, and it creates a make.bat file that calls make.exe -fMakefile.bcc

Here's a typical created .bcc file
# Borland's C++ Compiler 5.5 Makefile
 
CC       = bcc32
ILINK32  = ilink32
IMPLIB   = implib
TLIB     = tlib
RM       = del
BCCDIR   = C:\Borland\BCC55
 
CFLAGS   = -5 -O2 -tW -w-aus -w-ccc -w-csu -w-par -w-pia -w-rch -w-inl -w-ngu -w-pro -vu
LDFLAGS  = -V4.0 -c -x -Gn
DEFS     = -DNDEBUG -DWIN32 -DWINVER=0x0501 -DUNICODE -DHAVE_LOCAL_LIBCSTRING -DHAVE_LOCAL_LIBCERROR -DHAVE_LOCAL_LIBCERROR -DHAVE_LOCAL_LIBCTHREADS -DHAVE_LOCAL_LIBCDATA -DHAVE_LOCAL_LIBCLOCALE -DHAVE_LOCAL_LIBCNOTIFY -DHAVE_LOCAL_LIBCSPLIT -DHAVE_LOCAL_LIBUNA -DHAVE_LOCAL_LIBCFILE -DHAVE_LOCAL_LIBCPATH -DHAVE_LOCAL_LIBBFIO -DHAVE_LOCAL_LIBFCACHE -DHAVE_LOCAL_LIBFDATA -DHAVE_LOCAL_LIBFVALUE -DHAVE_LOCAL_LIBMFDATA -DHAVE_LOCAL_LIBCRYPTO -DHAVE_LOCAL_LIBHMAC -DHAVE_LOCAL_LIBCAES -DLIBEWF_DLL_EXPORT -DZLIB_DLL
INCLUDES = -I..\include;..\common;..\libcstring;..\libcerror;..\libcerror;..\libcthreads;..\libcdata;..\libclocale;..\libcnotify;..\libcsplit;..\libuna;..\libcfile;..\libcpath;..\libbfio;..\libfcache;..\libfdata;..\libfvalue;..\libmfdata;..\libhmac;..\libcaes;..\..\zlib;.;$(BCCDIR)\Include;
 
.SUFFIXES: .c
 
.c.obj:
	$(CC) -c $(INCLUDES) $(CFLAGS) $(DEFS) $<
 
all:	libewf
 
clean:
	$(RM) *.exe *.dll *.lib *.obj
 
libewf_SOURCES = libewf.c libewf_chunk_data.c libewf_chunk_table.c libewf_compression.c libewf_date_time.c libewf_date_time_values.c libewf_debug.c libewf_empty_block.c libewf_error.c libewf_filename.c libewf_file_entry.c libewf_handle.c libewf_hash_sections.c libewf_hash_values.c libewf_header_sections.c libewf_header_values.c libewf_io_handle.c libewf_legacy.c libewf_media_values.c libewf_metadata.c libewf_notify.c libewf_read_io_handle.c libewf_section.c libewf_sector_range.c libewf_segment_file.c libewf_segment_file_handle.c libewf_segment_table.c libewf_single_files.c libewf_single_file_entry.c libewf_single_file_tree.c libewf_support.c libewf_write_io_handle.c 
 
libewf_OBJECTS = $(libewf_SOURCES:.c=.obj)
 
libewf_LIBADD  = ..\libcstring\libcstring.lib ..\libcerror\libcerror.lib ..\libcthreads\libcthreads.lib ..\libcdata\libcdata.lib ..\libclocale\libclocale.lib ..\libcnotify\libcnotify.lib ..\libcsplit\libcsplit.lib ..\libuna\libuna.lib ..\libcfile\libcfile.lib ..\libcpath\libcpath.lib ..\libbfio\libbfio.lib ..\libfcache\libfcache.lib ..\libfdata\libfdata.lib ..\libfvalue\libfvalue.lib ..\libmfdata\libmfdata.lib ..\..\zlib\zlib.lib 
 
libewf: $(libewf_LIBADD) $(libewf_OBJECTS)
	$(RM) libewf.dll libewf.lib
	$(ILINK32) -Tpd -j$(BCCDIR)\Lib -L$(BCCDIR)\Lib $(LDFLAGS) $(libewf_OBJECTS) c0d32w.obj, libewf.dll, , $(libewf_LIBADD) import32.lib cw32.lib, ,
	$(IMPLIB) libewf.lib libewf.dll


and here are my own unedited notes
---------------------
- BCC Installation -
---------------------
 
Download the free compiler here:
http://forms.embarcadero.com/BCC32CompilerDownload
 
The resulting make.bat file (see further) requires you to install BCC in this folder:
C:\Borland\BCC55
Because the make.bat and Makefile.bcc files contains hard coded paths to this bcc location 
 
Install MinGW (see Compile LibEWF with MinGW instructions)
Because we need Unix tools to execute an *.sh file (generate_bcc32.sh)  
However Cygwin is also an option if you have it and know your way around it.
 
Install the patch command in MinGW (In a DOS box / CMD) :
mingw-get install msys-patch
Because (at least for now) some of the existing code needs to be changed to work with BCC
 
-------------------
- LibEWF Download -
-------------------
 
Download a stable version here: https://googledrive.com/host/0B3fBvzttpiiSMTdoaVExWWNsRjg/  (e.g. libewf-20140608.tar.gz )
 
---------------------
- Also download -
---------------------
 
http://zlib.net/zlib-1.2.8.tar.gz
https://googledrive.com/host/0B3fBvzttpiiSMTdoaVExWWNsRjg/libewf-20140608.tar.gz (or other version)
https://github.com/libyal/libyal/blob/master/generate_bcc32.sh (go here: https://github.com/libyal/libyal, click download Zip button and get it out of the downloaded zip file)
libewf-20140608-bcc.patch from https://googledrive.com/host/0B3fBvzttpiiSMTdoaVExWWNsRjg/
 
---------------------
- Prep -
---------------------
 
Unpack following two zips in a folder (for instance /bcc_compile/ )
zlib-1.2.8.tar.gz to /bcc_compile/zlib (make sure it's /zlib/ without the version number)
libewf-20140608.tar.gz (or other version)
 
Copy following two files in the project folder /bcc_compile/libewf-20140608/ (or other version) :
generate_bcc32.sh
libewf-bcc.patch
 
Start MingW console (called MSYS) via CMD:
	M:\MinGW\msys\1.0\msys.bat
	
Continue in MSYS and navigate to the folder where you put the files
	cd c:\users\peter\desktop\bcc_compile\libewf-20140608
	
Execute following commands in MSYS:
patch -p1 < libewf-bcc.patch
sh generate_bcc32.sh
 
Navigate to the LibEWF code in a DOS box (CMD), (not in MSYS)
cd c:\users\peter\desktop\bcc_compile\libewf-20140608
 
Before you execute make.bat you may want to edit it a bit in case you installed Bcc5.5 in a different folder than the default folder and/or if you have other versions of bcc installed (e.g. another Borland / Codegear / Embarcadero compiler).  For instance edit the PATH to only use the PATH to the /bcc55/bin/ folder, so that no other binaries are used (e.g. bcc32.exe from a different location): set PATH=C:\Borland\BCC55\bin
 
In the DOS box (CMD) execute:
make.bat
 
The resulting libewf.dll will appear in \libewf-20140608\libewf\
You also need zlib.dll from \zlib\  (libewf.dll uses zlib.dll)


During this process I don't run into .DEF files that I can edit, any idea at what point they are created or do I need to add them myself and use them somehow ?

Edited by: Peter Van Hove on Jan 5, 2015 12:45 PM
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Remove leading underscores from functions in dll  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 5, 2015 12:59 PM   in response to: Peter Van Hove in response to: Peter Van Hove
Peter wrote:

I see. During the building process, as far as I can see, no *.DEF
files are created.

That is correct. You have to provide your own .DEF file if you want to modify
the exported index/name values.

Is this something I need to add

Yes.

and how do I go about this ?

You were already told how. Compile the DLL once, then use the IMPDEF tool
to create a .DEF file from the DLL, customize it as needed, add it to your
project, and recompile.

--
Remy Lebeau (TeamB)
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Remove leading underscores from functions in dll [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 5, 2015 1:21 PM   in response to: Peter Van Hove in response to: Peter Van Hove
Peter wrote:

What happens is that I run a script that creates Makefile.bcc files
from existing source code, and it creates a make.bat file that calls
make.exe -fMakefile.bcc

You will have to create the .def file and then edit the .bcc file to specify
the .def file as a parameter to ilink32.

During this process I don't run into .DEF files that I can edit, any
idea at what point they are created

They are not created at any point in that script. You have to create it
yourself.

or do I need to add them myself and use them somehow ?

Yes. You will have to make it ahead of time and add it to the project.
If you are dynamically creating the .bcc file each time, then you will have
to update the generator to include scripting that compiles the DLL once,
then calls IMPDEF to create the DEF file, then modifies the DEF file as needed,
and then re-compiles the DLL again with the DEF file.

--
Remy Lebeau (TeamB)
Peter Van Hove

Posts: 48
Registered: 5/7/09
Re: Remove leading underscores from functions in dll [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 5, 2015 5:17 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Thanks Remy, I'll see what I can do.
Cheers.
Peter Van Hove

Posts: 48
Registered: 5/7/09
Re: Remove leading underscores from functions in dll [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 6, 2015 7:10 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
The use
of underscores in name-mangled exports is part of the __cdecl calling convention,
it cannot be disabled, only hidden with a .DEF file.

According to the author the use of __declspec(dllexport) suppresses the underscore, or so it does with VC++ (which I cannot verify)
Is this something that is ignored by bcc32 or does it serve a different use ?

#define LIBEWF_EXTERN __declspec(dllexport)
 
#if defined( __cplusplus )
extern "C" {
#endif
 
LIBEWF_EXTERN \
void libewf_error_free(
      libewf_error_t **error );
 
#if defined( __cplusplus )
}
#endif


I also noticed that __cplusplus is not defined during the building, and extern "c" is seen as an error (when I remove the #ifdef), which confuses me.
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Remove leading underscores from functions in dll [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 7, 2015 10:03 AM   in response to: Peter Van Hove in response to: Peter Van Hove
Peter wrote:

According to the author the use of __declspec(dllexport) suppresses
the underscore, or so it does with VC++ (which I cannot verify)

__declspec(dllexport) does not suppress underscores, and neither does __export.

I also noticed that __cplusplus is not defined during the building,
and extern "c" is seen as an error (when I remove the #ifdef),
which confuses me.

extern "C" tells the compiler/linker to create external linkages for code
using C rules rather than C++ rules. One effect of doing that is decoration
of exported names is disabled. For instance, without extern "C", the compiler
would decorate the names with extra information that describes the calling
convention, parameters list and return type. You kind of information is
needed for function overloading, but you don't want that information present
when exporting DLL functions.

See http://stackoverflow.com/a/1041880/65863 for more information.

The #ifdef is so the code can detect when it is being compiled as C or C++.
It does not make sense to use extern "C" when compiling as C, only when
compiling as C++. The compiler defines __cplusplus when compiling in C++.

--
Remy Lebeau (TeamB)
Andreas Muchow

Posts: 35
Registered: 12/20/12
Re: Remove leading underscores from functions in dll  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 29, 2015 9:10 AM   in response to: Peter Van Hove in response to: Peter Van Hove
Peter Van Hove wrote:
All works, due to the work of the library author, but the functions are exported with an underscore and for the sake of conformity with other compilers we need to see the functions without underscores.
[...]
-vu

"-vu" should have worked in general, it does for my XE version of C++-Builder. It has a corresponding project file setting and that documents "-vu" and I have a project currently where I need exactly that and it behaves correctly, my exported function doesn't contain a leading underscore.
daisy eden

Posts: 5
Registered: 3/4/16
Re: Remove leading underscores from functions in dll  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 1, 2016 2:03 AM   in response to: Andreas Muchow in response to: Andreas Muchow
acemary mary

Posts: 12
Registered: 2/23/16
Re: Remove leading underscores from functions in dll  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 10, 2016 11:45 PM   in response to: Peter Van Hove in response to: Peter Van Hove
I have a DLL that I've written which contains functions intended for use with a custom TestStand step type. Here is an example prototype:

void __declspec(dllexport) TX_TEST RfSwConnectConfig(tTestData *testData, tTestError *testError) ;

The problem is that the names of the exported functions changed in the most recent build of the DLL. They were originally exported with a leading underscore, such as _RfSwConnectConfig. Now the underscore is missing, RfSwConnectConfig. I can't seem to find any compiler options which would control this. The new builds are occuring on a different computer, so the environment is not the same. I'm currently using LabWindows/CVI 7.1. I can update my TestStand scripts to call either way, but would like to ensure that all future builds export the same function names. http://www.traininginsholinganallur.in/php-training-in-chennai.html | http://www.besanttechnologies.com/training-courses/php-training/php-training-institute-in-chennai | http://www.trainingintambaram.in/php-training-in-chennai.html
acemary mary

Posts: 12
Registered: 2/23/16
Re: Remove leading underscores from functions in dll  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 8, 2016 5:47 AM   in response to: Peter Van Hove in response to: Peter Van Hove
My guess would be that the two machines are in different compatibility modes. Functions compiled in Visual C/C++ mode will have a prepended underscore while those compiled in Borland C/C++ mode will not. To check this, go to Options...->Build Options...->Compatibility with:. Vmware Training in Chennai | Web Designing Training in Chennai | Vmware Training in Chennai
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02