473,785 Members | 2,784 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

avoiding multiple definition error


Your C program is using library libA which has a structure
struS.

You need to modify struS but may not touch the library. How
can your program redefine the structure struS while keeping
the compiler happy?

Jan 11 '07 #1
3 2029
jo*******@lycos .com wrote:
Your C program is using library libA which has a structure
struS.

You need to modify struS but may not touch the library. How
can your program redefine the structure struS while keeping
the compiler happy?
It can't.

--
Ian Collins.
Jan 11 '07 #2
On 10 Jan 2007 23:51:26 -0800, jo*******@lycos .com wrote:
>
Your C program is using library libA which has a structure
struS.

You need to modify struS but may not touch the library. How
can your program redefine the structure struS while keeping
the compiler happy?
This could be accomplished if struS is declared with external linkage
and its type is visible to your C program. Something like the
following snippets:

/* libA.h */
struct foo {int bar;}

/* libA.c */
struct foo struS;

/* yourcprogram.c */
extern struct foo struS;
struS.bar = 0;

Note that if struS was defined with internal linkage (like it arguably
should have been):

/* libA.c */
static struct foo struS;

then there is no standard way to modify struS without "touching the
library" (e.g., calling a function that modifies struS).

Regards
--
jay
Jan 11 '07 #3
On Wed, 10 Jan 2007 23:51:26 -0800, joe.user0 wrote:
Your C program is using library libA which has a structure struS.
You need to modify struS but may not touch the library. How can your
program redefine the structure struS while keeping the compiler happy?
Your subject implies a name conflict, that is, you want to name
something struS that's unrelated to libA, but use other features
of libA. In that case, just rename the struct when you include the
header file

#define struS libAstruS
#include <libAheader.h >
#undef struS

If you really mean you want to modify struS to your own taste, the
standard allows you _only_ to add fields to the end _of your own copies_
of struS:

#define struS libAstruS
#include <libAheader.h >
#undef struS

--- then either (A):

struct struS {
... libA fields of struS ...
... your new fields ...
};

--- or (B):

struct struS {
libAstruS libAbits;
... your new fields ...
};

The second form protects you from changes to the library's definition,
and allows you to call properly prototyped functions more cleanly.
With (A), you'll have to cast:

struct struS mystruS;
libAfunc((struc t libAstruS *) &mystruS);

with (B), just name the library's part:

struct struS mystruS;
libAfunc(&mystr uS.libAbits);
Neither solution is more to my taste than leaving the library name alone
and incorporating the libA struS into a struct of your very own (C):

stuct mystruS {
struS libAstruS libAbits;
... your new fields ...
};

struct mystruS mystruS;
libAfunc(&mystr uS.libAbits);
In any of the cases, you'll have to do some fiddling if the library
creates struSes. For example, if the libA header declares a function
struct struS libAnew_struS(v oid);
(A)
struct struS mystruS;
struct libAstruS *pstruS = (struct libAstruS *) &mystruS;

*pstruS = libAnew_struS() ;

(B)
struct struS mystruS;
mystruS.libAbit s = libAnew_struS() ;

(C)
struct mystruS mystruS;
mystruS.libAbit s = libAnew_struS() ;

If the library exports global struSes, you're out of luck entirely.
eg, given (B)

extern struS gblstruS;

gblstruS.libAfi eld = xxx; /* OK */
gblstrus.newfie ld = xxx; /* abort, if you're very lucky */

In that case, declare your own structs containing pointers to libA struSes.

Martin
--
Martin Golding DoD #0236 | fo*****@comcast .net
Always code as if the person who ends up maintaining your code will be a
violent psychopath who knows where you live.

Jan 12 '07 #4

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

Similar topics

11
17835
by: Georg Teichtmeister | last post by:
Hello! We are developing a math - library for realtime applications and want to use some given mathlibraries as base(ipp, MTL, .. ). Our library is a wrapper for those and you should be able to select the underlying library by template parameter. Therefore we split up our library in two namespaces: The first one defines namespace-global functions which call the baselibrary functions. These are templatefunctions which some
5
2477
by: Charles L | last post by:
Can someone explain to me what the following means? "C permits multiple definitions of a variable in any given namespace, provided the definitions are the same and it generates only a single variable for the multiple definitions. C++, however, does not permit redefinition of a variable or any other entity for a very definite reason that we will discuss later." Chapter 1. C++ Tutorial. Coronado Enterprises Charles L
3
4646
by: Dave | last post by:
Hello all, Please consider this code: #ifndef FOO_INCLUDED #define FOO_INCLUDED // File: foo.h class foo {
9
2794
by: lbj137 | last post by:
I have two files: A.c and B.c. In both files I define a global variable, int xxxx; When I compile with a green hills compiler (and also i think with a GNU compiler) I get no errors or warnings. Only when I initialize xxxx to two different values in A.c and B.c respectively do I get a 'multiple definitions' error. I was under the impression that I would get such an error even without initializing xxxx twice, simply because i am (ostensibly)...
0
645
by: jhe | last post by:
I have a user control HeaderControl which was shared by multiple Asp.Net Pages (.aspx), sometimes when I make some changes to our website and restart iis, it will have the CS1595 error, but when I change something (for example, add a space) in the HeaderControl.ascx, it will work correctly. Here is the specific error message: ============== Compilation Error Description: An error occurred during the compilation of a
11
22584
by: lars.uffmann | last post by:
Easily described problem: Using g++ version 3.3.5 under suse 9.3, bla.h: ----------- #ifndef myTEST #define myTEST ZFSInt test; #endif
3
2148
by: Jens Müller | last post by:
I have a file here with several enums: #ifndef PLANARSEP_OPTIMIZE_H #define PLANARSEP_OPTIMIZE_H enum fund_cycle_behavior_t {PASS_MODE_FIRST, PASS_MODE_BEST, PASS_MODE_ALL};
10
2815
by: zfareed | last post by:
Similar problem to the previous post. I have created a project with about 7 files including 3 header files and 3 implementation files. I am getting a multiple definition error when compiling for one specific file of class. I have checked that if there is more than one allocation of storage for identifiers, this error arises. But I don't see where the problem lies. Any suggestions? <code> //ItemType.h #ifndef ItemType_hpp // I was...
6
2421
by: Gaijinco | last post by:
I'm having a weird error compiling a multiple file project: I have three files: tortuga.h where I have declared 5 global variables and prototypes for some functions. tortuga.cpp where I implement all of the functions of tortuga.h main.cpp where I use the functions implemented in tortuga.cpp I create the objetc file without problem. But when I try to compile
0
9645
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
10329
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
10092
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
9950
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
7500
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.