473,781 Members | 2,683 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

(Silly?) question about linkage of C applications

I have an application written in C (actually PostgreSQL). The
application appears to have been built using the Mingw set of tools
(mingw compiler tools).

I want to write an extension library library to be used by this
application. Do I have to use the same compiler used to create the
application - or is this irrelevant - i.e. can I use ANY compiler to
write the extension library.

The larger question I want to ask really (for the purists amongst you
who will think this is a platform specific question) is:

Can a C application built with one compiler load and use a shared
library built by another compiler?

If the answer is no, I will be very grateful to know what the technical
reasons are.

Jul 8 '06 #1
12 1518
Bit byte wrote:
I have an application written in C (actually PostgreSQL). The
application appears to have been built using the Mingw set of tools
(mingw compiler tools).

I want to write an extension library library to be used by this
application. Do I have to use the same compiler used to create the
application - or is this irrelevant - i.e. can I use ANY compiler to
write the extension library.

The larger question I want to ask really (for the purists amongst you
who will think this is a platform specific question) is:

Can a C application built with one compiler load and use a shared
library built by another compiler?

If the answer is no, I will be very grateful to know what the technical
reasons are.
In general, on the same platform yes they can. Otherwise, how would you
link the platform's libraries?

--
Ian Collins.
Jul 8 '06 #2
Ian Collins wrote:
Bit byte wrote:
I have an application written in C (actually PostgreSQL). The
application appears to have been built using the Mingw set of tools
(mingw compiler tools).

I want to write an extension library library to be used by this
application. Do I have to use the same compiler used to create the
application - or is this irrelevant - i.e. can I use ANY compiler to
write the extension library.

The larger question I want to ask really (for the purists amongst you
who will think this is a platform specific question) is:

Can a C application built with one compiler load and use a shared
library built by another compiler?

If the answer is no, I will be very grateful to know what the technical
reasons are.
In general, on the same platform yes they can. Otherwise, how would you
link the platform's libraries?
Even on the same platform, not necessarily. The stack alignment forced
and assumed at function calls may be different, as a real-world example.

Jul 8 '06 #3
>I want to write an extension library library to be used by this
>application. Do I have to use the same compiler used to create the
application - or is this irrelevant - i.e. can I use ANY compiler to
write the extension library.
It doesn't make sense to use (cross) compilers targeted to different
CPUs for different parts of the program to be linked together except
in EXTREMELY RARE circumstances.

There is no guarantee that any linker recognizes the object file
format of any two compilers you choose. (On CP/M, there were a
number of different and highly incompatible object formats from
different vendors) However, there is a fairly good chance that for
any CPU and OS target, compilers for that target will have one
standard object format, especially if they all use the system linker,
assuming there is one supplied with the OS.

There is no guarantee that object created in different modes of
the *SAME* compiler can be meaningfully linked together.
You probably have to use the same options on all linked pieces
(including the system libraries) for:
- 32 vs. 64 bit mode
- packed/non-packed structures
- memory model (small/medium/compact/large/huge/gigantic)
- symbol name-mangling mode (e.g. compiling C vs. C++)
- linkage conventions (among other issues, there are
"pascal" vs. "C" calling conventions in MS-DOS)
- position-independent code vs. not.

>The larger question I want to ask really (for the purists amongst you
who will think this is a platform specific question) is:

Can a C application built with one compiler load and use a shared
library built by another compiler?
There's no guarantee that a given compiler can be used to create a
shared library, period, even if the OS supports them. For example,
on some systems, shared libraries require position-independent code,
and a compiler might not support that.
>If the answer is no, I will be very grateful to know what the technical
reasons are.
There are lots of low-level details about how shared libraries
work, including linkage conventions, that need not work the same
in all compilers.

Gordon L. Burditt
Jul 8 '06 #4
Harald van Dijk wrote:
Ian Collins wrote:
>>Bit byte wrote:
>>>I have an application written in C (actually PostgreSQL). The
applicatio n appears to have been built using the Mingw set of tools
(mingw compiler tools).

I want to write an extension library library to be used by this
applicatio n. Do I have to use the same compiler used to create the
applicatio n - or is this irrelevant - i.e. can I use ANY compiler to
write the extension library.

The larger question I want to ask really (for the purists amongst you
who will think this is a platform specific question) is:

Can a C application built with one compiler load and use a shared
library built by another compiler?

If the answer is no, I will be very grateful to know what the technical
reasons are.

In general, on the same platform yes they can. Otherwise, how would you
link the platform's libraries?


Even on the same platform, not necessarily. The stack alignment forced
and assumed at function calls may be different, as a real-world example.
OK, I'll specialise a bit more - on most UNIX platforms (where there the
OS linker is used) they can.

--
Ian Collins.
Jul 8 '06 #5
Ian Collins wrote:
Harald van Dijk wrote:
Ian Collins wrote:
>Bit byte wrote:

I have an application written in C (actually PostgreSQL). The
application appears to have been built using the Mingw set of tools
(mingw compiler tools).

I want to write an extension library library to be used by this
application . Do I have to use the same compiler used to create the
application - or is this irrelevant - i.e. can I use ANY compiler to
write the extension library.

The larger question I want to ask really (for the purists amongst you
who will think this is a platform specific question) is:

Can a C application built with one compiler load and use a shared
library built by another compiler?

If the answer is no, I will be very grateful to know what the technical
reasons are.
In general, on the same platform yes they can. Otherwise, how would you
link the platform's libraries?

Even on the same platform, not necessarily. The stack alignment forced
and assumed at function calls may be different, as a real-world example.
OK, I'll specialise a bit more - on most UNIX platforms (where there the
OS linker is used) they can.
Stack alignment is a compiler issue, not a linker issue. The problem is
with code as simple as

void f(void) {
int a[4] = { 0 };
}

If a compiler can assume a four word stack alignment, it may be able to
use a special instruction to zero all of a[0 ... 3] at once. If another
compiler calls f(), but simply word-aligns the stack, the special
instruction may cause a runtime abort. I'm not sure how the linker
could change this.

Jul 8 '06 #6
Harald van Dijk wrote:
Ian Collins wrote:
>>Harald van Dijk wrote:
>>>Ian Collins wrote:
Bit byte wrote:
>I have an application written in C (actually PostgreSQL). The
>applicatio n appears to have been built using the Mingw set of tools
>(mingw compiler tools).
>
>I want to write an extension library library to be used by this
>applicatio n. Do I have to use the same compiler used to create the
>applicatio n - or is this irrelevant - i.e. can I use ANY compiler to
>write the extension library.
>
>The larger question I want to ask really (for the purists amongst you
>who will think this is a platform specific question) is:
>
>Can a C application built with one compiler load and use a shared
>library built by another compiler?
>
>If the answer is no, I will be very grateful to know what the technical
>reasons are.
>

In general, on the same platform yes they can. Otherwise, how would you
link the platform's libraries?
Even on the same platform, not necessarily. The stack alignment forced
and assumed at function calls may be different, as a real-world example.

OK, I'll specialise a bit more - on most UNIX platforms (where there the
OS linker is used) they can.


Stack alignment is a compiler issue, not a linker issue. The problem is
with code as simple as

void f(void) {
int a[4] = { 0 };
}

If a compiler can assume a four word stack alignment, it may be able to
use a special instruction to zero all of a[0 ... 3] at once. If another
compiler calls f(), but simply word-aligns the stack, the special
instruction may cause a runtime abort. I'm not sure how the linker
could change this.
Well I've never seen any problems on any of the UNIX/Linux systems I
have used, after all, not matter what compiler you use, you still have
to link (and work!) with the system's libraries.

--
Ian Collins.
Jul 8 '06 #7
On 2006-07-08, Bit byte <fl**@flop.comw rote:
I have an application written in C (actually PostgreSQL). The
application appears to have been built using the Mingw set of tools
(mingw compiler tools).

I want to write an extension library library to be used by this
application. Do I have to use the same compiler used to create the
application - or is this irrelevant - i.e. can I use ANY compiler to
write the extension library.

The larger question I want to ask really (for the purists amongst you
who will think this is a platform specific question) is:

Can a C application built with one compiler load and use a shared
library built by another compiler?

If the answer is no, I will be very grateful to know what the technical
reasons are.
There's no guarantee, the two things that are most likely to go wrong:

1. Calling convention (how parameters are passed to functions). But in
practice these days many toolchains use cdecl (a particular calling
convention) so you usually get away with it. Many compilers provide
some non-standard decorations you can put around function definitions
and declarations to ensure a particular convention is used. ("extern
__cdecl" that kind of thing). So you may need to add decorators to
the declarations in the headers of the library you're extending, or
to the definitions of functions in your code that the library calls
back.
2. Structure packing. Suppose you write:

struct thing
{
int x;
char y;
int z;
};

in a header file, and include this in sources you build with two
different compilers. Each compiler may have a different idea how to
lay out the struct in memory (different amounts of padding between
and after the members). This can cause nasty problems if code in the
two modules ever shares an instance of one of the structures.

This is best avoided by not putting struct definitions in headers,
but you can't help it if the code you're extending did. Compilers
often have extensions like #pragma pack so you can tweak your
compiler so it packs structs the same as the mingw compiler used to
build the other stuff.

So, it is usually possible if you have to, but you might need to use
some non-standard compiler extensions.

This has always been a problem for people who wanted to ship an SDK in
binary form without mandating a particular compiler. Nowadays it's
becoming more common for people to ship the source with the SDK so the
customer just builds everything with their choice of compiler. It means
the SDK people have to write portable C, which is no bad thing, and in
other ways is generally better for everyone.
Jul 8 '06 #8
Ian Collins wrote:
Harald van Dijk wrote:
Ian Collins wrote:
>Harald van Dijk wrote:

Ian Collins wrote:
Bit byte wrote:
I have an application written in C (actually PostgreSQL). The
applicati on appears to have been built using the Mingw set of tools
(mingw compiler tools).

I want to write an extension library library to be used by this
application . Do I have to use the same compiler used to create the
applicati on - or is this irrelevant - i.e. can I use ANY compiler to
write the extension library.

The larger question I want to ask really (for the purists amongst you
who will think this is a platform specific question) is:

Can a C application built with one compiler load and use a shared
library built by another compiler?

If the answer is no, I will be very grateful to know what the technical
reasons are.
In general, on the same platform yes they can. Otherwise, how would you
link the platform's libraries?
Even on the same platform, not necessarily. The stack alignment forced
and assumed at function calls may be different, as a real-world example.
OK, I'll specialise a bit more - on most UNIX platforms (where there the
OS linker is used) they can.

Stack alignment is a compiler issue, not a linker issue. The problem is
with code as simple as

void f(void) {
int a[4] = { 0 };
}

If a compiler can assume a four word stack alignment, it may be able to
use a special instruction to zero all of a[0 ... 3] at once. If another
compiler calls f(), but simply word-aligns the stack, the special
instruction may cause a runtime abort. I'm not sure how the linker
could change this.
Well I've never seen any problems on any of the UNIX/Linux systems I
have used, after all, not matter what compiler you use, you still have
to link (and work!) with the system's libraries.
All four word-aligned stacks are also word-aligned, so if that's what
the system library assumes, no problem. The standard library would need
to use platform-specific hacks to only add multiples of 4*sizeof(int)
to the stack pointer if callback functions are used, but that's
certainly possible to do, even automatically, depending on the
compiler. I can't find a concrete example of this right now, though.

Another possibility for incompatibiliti es are in how large structs are
returned from functions, when the standard library only returns small
structs, or what size enum types and _Bool have, when the standard
library doesn't rely on those. Both of those are extremely easy to find
examples of, even, since GCC lets you change these (except for _Bool)
using compiler options.

Jul 8 '06 #9
Harald van Dijk a écrit :
Ian Collins wrote:
>>Bit byte wrote:
>>>I have an application written in C (actually PostgreSQL). The
applicatio n appears to have been built using the Mingw set of tools
(mingw compiler tools).

I want to write an extension library library to be used by this
applicatio n. Do I have to use the same compiler used to create the
applicatio n - or is this irrelevant - i.e. can I use ANY compiler to
write the extension library.

The larger question I want to ask really (for the purists amongst you
who will think this is a platform specific question) is:

Can a C application built with one compiler load and use a shared
library built by another compiler?

If the answer is no, I will be very grateful to know what the technical
reasons are.

In general, on the same platform yes they can. Otherwise, how would you
link the platform's libraries?


Even on the same platform, not necessarily. The stack alignment forced
and assumed at function calls may be different, as a real-world example.
No, under Unix there is the ABI (Application Binary Interface) and under
windows there is the implicit ABI of Microsoft. Stack alignment anyway
is dictated by the processor and not the ABI.
Jul 8 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
12144
by: qazmlp | last post by:
const has internal linkage in C++, but external linkage in C. Am I right ? But, linker reports multiply-defined error if the following header is included in multiple .cpp files. // test_const.h #ifndef HEADER_TEST #define HEADER_TEST
3
1209
by: Roman Mashak | last post by:
Hello, All! As far as I understand, function declaration with 'static' keyword limits the access to these functions within the file where it declared. So, my question is: where can it be used in real applications? Thanks in advance. With best regards, Roman Mashak. E-mail: mrv@tusur.ru
11
1610
by: Sensei | last post by:
Hi again! I have still curiosity about the reason of some C constructs/keywords... The first is about static functions. What was the reason of restricting a function to be visible just in a specific source file? Wasn't it sufficient not to be given a prototype (for visibility)? What about register and volatile variables? Was at that time a compiler not smart enough to optimize with in-register variables? And why would
3
6036
by: al.cpwn | last post by:
do static and inline functions or members have internal linkage? I have been reading this newsgroup on google and found conflicting ideas. Can someone please help me understand why in some places inline may have external linkage while in others it has internal (from what I have read in comments by people). Are there any (other) places where linkage is ambiguous?
13
2097
by: fctk | last post by:
source: http://rm-f.net/~orange/devel/specifications/c89-draft.html#3.1.2.2 there are two passages in this paragraph i can't fully understand: 1) "If the declaration of an identifier for an object or a function contains the storage-class specifier extern , the identifier has the same linkage as any visible declaration of the identifier with file scope. If there is no visible declaration with file scope, the identifier has external...
75
5482
by: Steven T. Hatton | last post by:
No, this is not a troll, and I am not promoting Java, C-flat, D, APL, Bash, Mathematica, SML, or LISP. A college teacher recently posted to this newsgroup regarding her observation that there has been a significant decline in the number of students opting to take courses in C++. I just want to let people know what I believe are the biggest obstacles to C++ language acquisition, and what aspects of the language make it less appealing than...
4
6260
by: mimi | last post by:
The programmer indicates to the compiler that a function is written in a different programming language using a linkage directives.It is intuitive that extern "SomeLanguage" is used to declare functions written in the "SomeLanguage". But I am quite confused what information does the linkage directive tells the compiler.The "generated" function name? The way the arguments are ordered?Or something else? And I am still wondering why extern...
1
1520
by: Kurt | last post by:
The C++ standard (in 3.5:6, p42Example) said: static int i = 0; // 1 void g() { int i; //2: i has no linkage { extern int i; //3: external linkage
5
1937
by: gw7rib | last post by:
I was having linking errors when I put: const LPCTSTR Main_window_name = _TEXT("Thingy_main_window"); in one file and extern const LPCTSTR Main_window_name; in another. I've since realised that this is because (in C++) consts do not, by default, have external linkage. I've solved the problem by
0
9639
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9474
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10308
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10076
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9939
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7486
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5375
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5507
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.