Cross platform dynamic libraries in "C" (Microsoft and gcc) - linux

Ultimately I want to end up with a single set of source files that compiles to a Windows or Linux dynamic library depending on which platform compiled it.
The problem seems to be that Windows requires that annotations be made to both the header file declarations and the source file definitions. DLL Tutorial For Beginners
Linux dynamic link libraries seem to require annotations only in the source file definitions.
I can #define a preprocessor string to handle the difference of the source code definitions.
#if (_MSC_VER >= 1900) // (Visual Studio 2015 version 14.0)
#define EXPORTED __declspec(dllexport)
#else
#define EXPORTED __attribute__((visibility("default")))
#endif
Both Windows and Linux ignore empty #define statements.

You may not use annotations in Windows. You can use DEF file for declare export functions .def files C/C++ DLLs
For linux you need to use annotations, e.g.:
int
#ifdef __GNUC__
__attribute__((visibility("default")))
#endif
myfunction(int param) {
return 0;
}

This solves the problem that Windows requires functions
in the DLL header and the DLL source file be annotated
and Linux requires only the LIB source functions to be
annotated. Both Windows and Linux ignore empty #define
statements.
//
// Dynamic Link Library for Linux and Windows
//
// If not >= Visual Studio 2015 (version 14.0) then
// Linux is assumed
//
// This file is inlcluded in all of the LIB/DLL Source
// and the LIB/DLL caller.
//
#if (_MSC_VER >= 1900) // (Visual Studio 2015 version 14.0)
// Windows Function Definition (LIB/DLL Source)
#define EXPORTED_DEF __declspec(dllexport)
// DLL_EXPORT is defined at top of LIB/DLL Source of exported functions
#if defined DLL_EXPORT
// Windows Function Declaration (DLL Header)
#define EXPORTED_DEC __declspec(dllexport) // DLL Export
#else
// Windows Function Declaration (Caller Header)
#define EXPORTED_DEC __declspec(dllimport) // DLL Import
#endif
#else
// Linux Function definition (LIB/DLL Source)
#define EXPORTED_DEF __attribute__((visibility("default")))
#define EXPORTED_DEC // Linux cannot see this
#endif

Related

Unresolved external when linking C++ Builder dll into MSVC C++ Project

I am trying to use a C++Builder 11.1.5 created DLL in MSVC 2022 C++ project. Both are 32bit. I can't build the DLL in MSVC as it uses some VCL objects.
My C++ Builder defines the external function thus:
extern "C" __declspec(dllexport) void __stdcall SetEnabled(bool val) {
...
}
MSVC uses a header to reference the function:
extern "C" __declspec(dllimport) void __stdcall SetEnabled(bool val) ;
I have created a .DEF file with the same name as the DLL, containing this:
LIBRARY MYTESTDLL
EXPORTS
SetEnabled
And I have generated a .LIB file from this .DEF file using MS lib.exe:
lib.exe /DEF:MYTESTDLL.def /out:MYTESTDLL.lib /MACHINE:x86
Finally, I have added MYTESTDLL.lib to my MSVC project in the Linker->Additional Dependencies section.
But, when I build the program, I get this error:
Error LNK2019 unresolved external symbol __imp__SetEnabled#4 referenced in function _WinMain#16
I've tried adding the ordinal (#4) to my .DEF file and rebuilding the .LIB file, but I still get the same error.
In a hex editor, I can that __imp__SetEnabled is in the .LIB file, but clearly not in a way that MSVC likes.
Have I missed a step somewhere? Is there anything obviously wrong with what I've done?
Changing the MSVC header used to reference the function to:
extern "C" __declspec(dllimport) void SetEnabled(bool val) ;
and adjusting the C++Builder DLL to export the functions using __cdecl resolved the issue. I didn't realize that the __stdcall exported functions need decorating in .DEF file.
That said, I find that the DLL is fine unless I try to execute code in it that uses the VCL. Then things don't work properly. I wonder if it's not possible to write a DLL in C++Builder containing VCL code for use in MSVC?

Using a regular dll with shared mfc dll

I have a regular dll with shared mfc dll. it was built on vc++ 6.0. I want to use it in vc++ 2008 windows form application.it has a header file, a lib and a dll.
I am adding some lines of the said header files
#ifdef DLLBUILD
#define DLLFUNC extern "C" __declspec(dllexport) WINAPI
#else
#define DLLFUNC extern "C" __declspec(dllimport) WINAPI
#endif
DLLFUNC int SC06StepInit(void)
When I add this header it shows multiple errors of same type
error C2144: syntax error : 'int' should be preceded by ';'
error
C4430: missing type specifier - int assumed. Note: C++ does not
support default-int
error start from line DLLFUNC int SC06StepInit(void) to all lines started with the same name DLLFUNC. I have no idea of starting any function with a name than return type and than again a name.
The source code of this dll also had all the functions in the same manner.
I want to know. What is this way of defining a function is called and to use such libraries.
Try this:
#ifdef DLLBUILD
#define DLLFUNC extern "C" __declspec(dllexport)
#else
#define DLLFUNC extern "C" __declspec(dllimport)
#endif
DLLFUNC int SC06StepInit(void);
You are trying too much in a macro:
extern "C"
DLL import/export attribute
WINAPI specification
Limit it. Find out how you should export the function, and who the client of your DLL are.

What is a keyword to dynamic library export / import in linux?

I'm developing a chat server.
this is my source.
#if defined(_WIN32) || defined(_WIN64)
#ifdef PENGCHAT3SERVERLIB_EXPORTS
#define PENGCHAT3SERVER_API __declspec(dllexport)
#else
#define PENGCHAT3SERVER_API __declspec(dllimport)
#endif
#elif defined(__unix) // is this right?
#define PENGCHAT3SERVER_API //....?????
#endif
I have two questions.
first, defined(__unix) is ok?
second, what is keyword dynamic library export / import in linux?
I'm very wondering.
Please help me
thanks.
If you're building on Linux, there is no need for a specifier or special measures; all function symbols are exported by default.
#define PENGCHAT3SERVER_API
You want to look for the __unix__ (preferred) or unix defines, but you should consider making it the default instead.

Problems with Defining exports

I am trying to build a Dynamic DLL in VC++ 2008, now in a .h file, I declare the following
#ifndef PREFILTER_LIBRARY_H
#define PREFILTER_LIBRARY_H
#ifdef PREFILTER_EXPORTS
# define PREFILTER_API __declspec(dllexport)
#else
# define PREFILTER_API __declspec(dllimport)
#endif
#endif
While in the PreFilter.h file I am writing
class PREFILTER_API PreFilter
{
...
};
The problem is I keep getting:
warning C4273: 'PreFilter::Apply' : inconsistent dll linkage
I see that the dllexport part of the above macros is not highlighted and is commented which should have been the other way around, plus I have another .h file that contains Apply() method.
Can't figure out what I am doing wrong here. I am trying to export the functions of PreFilter.h
Add PREFILTER_EXPORTS to the list of preprocessor constants in Dll project settings: Project - Properties - Configuration Properties - C++ - Preprocessor - Preprocessor definitions.
When this file is used in Dll project, PREFILTER_EXPORTS is defined in the project, and PREFILTER_API is expanded as __declspec(dllexport). In any other project, where PREFILTER_EXPORTS is not defined, PREFILTER_API is expanded as __declspec(dllimport).

Compiler #defines for g++ and cl

I am writing a program that is cross platform. There are a few spots where I have to specify an operating system dependent call.
#ifdef WINDOWS
..do windows only stuff
#endif
#ifdef LINUX
..do linux only stuff
#endif
Are there any preprocesser directives that get defined by the compiler so I don't have to explicitly define them when I use the command line compiler. ie.
cl -DWINDOWS program.cpp
or
g++ -DLINUX program.cpp
I realize I could easily write a makefile or have a shell/batch script that will do this automatically. But I would prefer to use the same ones as the compiler (if they exist) by default.
I found a complete list for all g++'s on this website.
#define __HAVE_BUILTIN_SETJMP__ 1
#define __unix__ 1
#define unix 1
#define __i386__ 1
#define __SIZE_TYPE__ unsigned int
#define __ELF__ 1
#define __GNUC_PATCHLEVEL__ 3
#define __linux 1
#define __unix 1
#define __linux__ 1
#define __USER_LABEL_PREFIX__
#define linux 1
#define __STDC_HOSTED__ 1
#define __EXCEPTIONS 1
#define __GXX_WEAK__ 1
#define __WCHAR_TYPE__ long int
#define __gnu_linux__ 1
#define __WINT_TYPE__ unsigned int
#define __GNUC__ 3
#define __cplusplus 1
#define __DEPRECATED 1
#define __GNUG__ 3
#define __GXX_ABI_VERSION 102
#define i386 1
#define __GNUC_MINOR__ 2
#define __STDC__ 1
#define __PTRDIFF_TYPE__ int
#define __tune_i386__ 1
#define __REGISTER_PREFIX__
#define __NO_INLINE__ 1
#define _GNU_SOURCE 1
#define __i386 1
#define __VERSION__ "3.2.3 20030502 (Red Hat Linux 3.2.3-47.3)"
And Visual C++ already has Win32 defined to use for any Widnows version.
Yes, there are such pre-defined symbols but I don't recommend you use them unless you never, ever, forsee supporting more platforms, compilers or operating system versions. Here's the logic.
First, you're better off with your own minimal set of defined compilation constants for use within your code. Because you might start with things like this:
#if defined(_WIN32)
// do windows stuff
#endif
#if defined(_linux)
// linux stuff
#endif
Suppose you allow compilation under a new compiler for windows that doesn't define _WIN32 automatically? You can change every _WIN32 line to something like:
#if defined(_WIN32) || defined(ming)
Which is a real pain in the butt if you have more than one of those to change. You could put something like this at a high level:
#if defined(ming)
#define _WIN32
#endif
But you'll likely discover that some system or library header file freaks out because they happen to use _WIN32. Better you had abstracted it in a common header file:
#if defined(_WIN32) || defined(ming)
#define PLAT_WINDOWS
#endif
And then simply use #if defined(PLAT_WINDOWS) where needed.
But what about that common header file? Well, when a new compiler/OS/whatever comes along you have to start tweaking it to make sure it says the right things. Pretty soon it is a comical hairball of condition that can only be understood if you know about every version of every compiler on every operating system on the planet. And gee, who doesn't but nevertheless any changes made have to be tested everywhere and that is painful.
So, better you just have some high level setting inside the makefile or even outside it that says "if you're on windows, -DPLAT_WINDOWS" and be done with it.
Of course, all this is minimized if you use the most commonly available functions and features in your code.
All of these answers were very good. The solution that worked for me was the following.
#ifdef _WIN32
#endif
#ifdef linux
#endif
WIN32 was not defined by cl but _WIN32 was.
linux was defined by g++.
Yes. I believe WIN32 is, but I'm not on a windows right now to test it :D
See:
http://en.wikipedia.org/wiki/C_preprocessor (it mentions WIN32)
Instead of having two macros for specific platforms, you may have the code in #ifndef #else block. For example:
#ifndef _WIN32
// linux specific code
#else
// windows specific code
#endif
With this solution, the ifndef-else block will make sure that you don't accidentally add code in between the two #ifdef blocks (which supposedly be handling the program execution flow in same manner).
The above code will also give you independence of compiling this code not just for linux but any unix platform, provided it supports the call, without changing the macro label from linux to unix or something else.

Resources