473,407 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

(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 1493
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
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.

--
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
>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.
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.comwrote:
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
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.
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 incompatibilities 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
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.
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
On Sat, 08 Jul 2006 11:17:39 +0200, in comp.lang.c , jacob navia
<ja***@jacob.remcomp.frwrote:
>Harald van D?k a écrit :
>Ian Collins wrote:
>>>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.
Apparently you've not come across the various options in the MS
compiler suite to change alignment. If you fool with this, you can
trivially generate link errors even with other MS libraries compiled
using hte same tools. And in days of yore, I recall frequently having
to tweak compiler options to ensure that MSC libraries would link with
Watcom C.

I've not tried this on Unix or Linux, but if the same flexibility
didn't exist, I'd be fairly surprised.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Jul 8 '06 #11
"Bit byte" <fl**@flop.comwrote in message
news:PZ********************@bt.com...
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.
It depends what the extension library does and how its linked etc.
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?
I would say in general "NO".
If the answer is no, I will be very grateful to know what the technical
reasons are.
Here are a few:-

1) The header files may be different. For example "<stdio.h>" then that
defines a "FILE" structure. They definition of "FILE" may well be different
between compilers. You may be able to get round this by using the header
files from the other compiler....

2) On some platforms, especially those that don't have a stack pointer
defined, e.g. IBM 370 Mainframe for one, the calling conventions are not
fixed. One compiler might generate code that use R11 as the stack pointer,
others R12.... Again on IBM because there is no PUSH/PULL one implementation
may move the stack pointer up, the other down ...

3) Then there are "helper functions". I don't think you will hit these in
Windows but on some platforms, especially those with emulated floating
point, some "C" expressions may invoke calls to "helper functions". You may
then find your self with missing routines.

Dave.
Jul 8 '06 #12
Harald van Dijk wrote:
Ian Collins wrote:
>>Harald van Dijk wrote:
>>>Ian Collins wrote:

Harald van Dijk wrote:

>Ian Collins wrote:
>>
>>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 incompatibilities 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.
All I can say is on my platform (Solaris), you would have to go out of
your way to introduce incompatibilities and your program probably
wouldn't link or run.

--
Ian Collins.
Jul 8 '06 #13

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

Similar topics

9
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. //...
3
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...
11
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...
3
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...
13
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...
75
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...
4
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...
1
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
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.