C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 12th, 2003, 05:58 PM
jbarker28 jbarker28 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Location: Minneapolis, MN
Posts: 5 jbarker28 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
How do I use a struct defined in a seperate .cpp file?

Hello,

I am trying to develop a library file that will be self-contained as much as possible...

so far, the only EXTERNAL reference I need to make is to a generic structure which would be defined in whichever main program is using the library...

ex:
Code:
////////////////////////
//main.h
///////////////////////

// standard includes, misc code...

struct Options
{
	string	s_param;
	int	i_param;
	bool	b_helpMd;
	bool	b_debugMd;
}; opt_obj


From within my proj_lib.h file, I am declaring this struct as extern, like so:
Code:
////////////////////////
//proj_lib.h
///////////////////////

extern struct Options opt_obj;


This is not working though...

I am trying to make this as 'pluggable' as possible, so that if I want to use this library in various programs, all I would need to do would be to #include it from within my main program header and create a struct object for it to utilize...

can this be done, or is there a better way of doing it?

Thanks!

Joshua B.

Reply With Quote
  #2  
Old May 12th, 2003, 08:40 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,406 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 10 h 17 m 19 sec
Reputation Power: 4080
Put the struct in your proj_lib.h file like so:
Code:
#ifndef __PROJ_LIB_H__
#define __PROJ_LIB_H__

struct Options
{
	string	s_param;
	int	i_param;
	bool	b_helpMd;
	bool	b_debugMd;
}; 

extern struct Options opt_obj;

#endif

Then within main.cpp and proj_lib.cpp, you can do:
#include "proj_lib.h"


Now, you can put the compiled code of proj_lib.cpp separately into a library. Any other program that needs to use that lib, simply needs to #include "proj_lib.h" to get the structure definition and/or opt_obj. Hope this helps

Last edited by Scorpions4ever : May 12th, 2003 at 08:45 PM.

Reply With Quote
  #3  
Old May 12th, 2003, 09:07 PM
jbarker28 jbarker28 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Location: Minneapolis, MN
Posts: 5 jbarker28 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hey scorpian, thanks for your help...

but actually, I was hoping there might be a way to do this so that I could avoid putting the struct definition within the library file...

what I would like to do (but don't know if it is possible) is make a self-contained .lib file which I could then link to any project I'm working on, and from program to program I could define any changeable run-tine options within the a structrue named Options placed in whatever main header is being used...

If I put the struct within my library file, then I can not edit or modify the option choices without manually going in and changing the library itself...

I don't know.. maybe I'm trying to do something that isn't normally done, or maybe I'm going about it the wrong way... this is only my first semester in C++, so anythings possible...

Anyhow, thanks again for the help!!

Joshua B.

Reply With Quote
  #4  
Old May 13th, 2003, 12:03 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,256 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 5 Days 20 h 36 m 23 sec
Reputation Power: 1985
Quote:
Originally posted by jbarker28
Hey scorpian, thanks for your help...

but actually, I was hoping there might be a way to do this so that I could avoid putting the struct definition within the library file...

...

There's a little trick I learned from C++ that may also work in C (oops, I just verified that you are talking C++). However, for it to work the other modules that use the struct cannot directly access its fields.

The idea is to make the extern variable a pointer to the struct. In C++, you can forward-declare a struct name then declare a pointer to that type of struct without having to worry about what's in that struct.

Usually, we would use it to include an object in another class without having to include that class's header file, because to do so would create a circular-reference mess. Here's an example header file for your case off the top of my head:
Code:
#ifndef _HEADER_H_
#define _HEADER_H_

// the forward declaration, just to establish the existence of a struct with that tag name.

struct aStruct;

// you establish that aPtr is a pointer to that struct
extern aStruct *aPtr;

#endif

aPtr will be initialized in your library code, which will be the only code module that needs to know the gory details about the struct. This is a desirable feature of OOP called "data hiding". And if other modules need access to certain fields within the struct, then write access methods for that.

That's right, you can treat the struct like you would a class. You can even write constructors and destructors for it. To my knowledge and from my own experience, a struct is roughly (or not so roughly) equivalent to a public class. Inheritance might be restricted, but I'm not sure about that.

Does it make sense and does it look like what you're looking for?

Last edited by dwise1_aol : May 13th, 2003 at 12:06 AM.

Reply With Quote
  #5  
Old May 13th, 2003, 01:22 AM
jbarker28 jbarker28 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Location: Minneapolis, MN
Posts: 5 jbarker28 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
That seems like a very interesting work-around...

I'll give it a try and let you guy's know how it works out...

Thanks

Joshua B.

Reply With Quote
  #6  
Old May 13th, 2003, 02:32 AM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 18 h 20 m 28 sec
Reputation Power: 14
Quote:
That's right, you can treat the struct like you would a class. You can even write constructors and destructors for it. To my knowledge and from my own experience, a struct is roughly (or not so roughly) equivalent to a public class. Inheritance might be restricted, but I'm not sure about that.


In C++, the only difference between a class and a struct is the default access specifier.

Reply With Quote
  #7  
Old May 13th, 2003, 08:59 AM
Doctor - A Doctor - A is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 11 Doctor - A User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally posted by dwise1_aol
There's a little trick

#ifndef _HEADER_H_
#define _HEADER_H_

// the forward declaration, just to establish the existence of a struct with that tag name.

struct aStruct;

// you establish that aPtr is a pointer to that struct
extern aStruct *aPtr;

#endif
[/code]

It is realy trick.
I prefer insert global objects in some common place.
For example, in the App cpp file in the MFC project.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > How do I use a struct defined in a seperate .cpp file?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap