Hello! I am working on dividing a single C file into several files.
Now I encounter a problem about the global variables
and can not find a way to solve it.
All global variables and codes used to be in that single file, that
worked OK. But when I divdie that file into several ones, I
have many "invalid use of undefined type" errors.
The four files are main.c, main.h, readLP.h, and readLP.c.
In readLP.h
I have:
#define MAX_NUMROWS 181010
#define MAX_NUMCOLS 201
#define MAX_NUMNZ 9000000
....
typedef struct Polytope {
int rmatbeg[MAX_NUMROWS];
int rmatind[MAX_NUMNZ];
double rmatval[MAX_NUMNZ];
double rhs[MAX_NUMROWS];
char sense[MAX_NUMROWS];
double cosine[MAX_NUMROWS];
double norm[MAX_NUMROWS];
/* char *rowname[MAX_NUMROWS]; */
} a_polytope, aa_polytope;
....
and in main.h, I have:
....
extern struct Polytope a_polytope, aa_polytope ;
in main.c , if the external variable a_polytope is
used as a function's parameters such as :
status = CPXaddrows (env, lp, 0, num_rows, num_nonzero,
a_polytope.rhs, a_polytope.sens e,a_polytope.rm atbeg,
a_polytope.rmat ind, a_polytope.rmat val, NULL, NULL);
/* this is a CPLEX callable library function */
I will get "invalid use of undefined type `struct
Polytope'" error in compiling.
It was working OK when all codes were in sigle file without using
keyword "extern".
If I change main.h into:
....
extern struct Polytope a_polytope, aa_polytope ;
struct Polytope *pa_polytope, *paa_polytope ;
....
and also change main.c into
pa_polytope = &a_polytope ;
status = CPXaddrows (env, lp, 0,num_rows, num_nonzero,
pa_polytope->rhs,pa_polytop e->sense,pa_polyt ope->rmatbeg,
pa_polytope->rmatind,pa_pol ytope->rmatval,
NULL, NULL);
I will get "dereferenc ing pointer to incomplete type"
four times for each pa_polytope->xxxx above.
Does anyone have a suggestion?
Thank you so much! 5 3303
PCHOME wrote: Hello! I am working on dividing a single C file into several files. Now I encounter a problem about the global variables and can not find a way to solve it. All global variables and codes used to be in that single file, that worked OK. But when I divdie that file into several ones, I have many "invalid use of undefined type" errors.
The four files are main.c, main.h, readLP.h, and readLP.c.
In readLP.h I have:
#define MAX_NUMROWS 181010 #define MAX_NUMCOLS 201 #define MAX_NUMNZ 9000000 ...
typedef struct Polytope { int rmatbeg[MAX_NUMROWS]; int rmatind[MAX_NUMNZ]; double rmatval[MAX_NUMNZ]; double rhs[MAX_NUMROWS]; char sense[MAX_NUMROWS]; double cosine[MAX_NUMROWS]; double norm[MAX_NUMROWS]; /* char *rowname[MAX_NUMROWS]; */ } a_polytope, aa_polytope;
Are you sure this is your code? It may be, of
course, but if so it doesn't mean what I suspect you
intend it to mean. This declaration says "Here is
what a `struct Polytope' looks like, and here are two
aliases for `struct Polytope' -- that is, any time I
write `a_polytope' or `aa_polytope' it is just as if
I had written out `struct Polytope' in full."
Note that this declaration does *not* declare or
define any variables of the type `struct Polytope' --
it just describes `struct Polytope' and creates two
aliases for it.
...
and in main.h, I have: ... extern struct Polytope a_polytope, aa_polytope ;
This says "Somewhere else I will define two variables
of type `struct Polytope' with these two names." However,
nothing that you've shown actually defines any such
variables.
Another interesting point (the proximal cause of the
diagnostics you're receiving) is that the description of
what a `struct Polytope' looks like appears only in
"readLP.h". If you include "main.h" in a file that doesn't
also include "readLP.h", that description is not available.
The compiler knows that `struct Polytope' is some kind of
a struct type, but doesn't know anything about the elements
that struct type contains: it doesn't know their names,
their types, their sizes, or their arrangement within the
struct. (You will eventually learn that such "incomplete
types" can be useful in certain circumstances, but I think
you may not be quite ready for that lesson yet.)
in main.c , if the external variable a_polytope is used as a function's parameters such as : status = CPXaddrows (env, lp, 0, num_rows, num_nonzero, a_polytope.rhs, a_polytope.sens e,a_polytope.rm atbeg, a_polytope.rmat ind, a_polytope.rmat val, NULL, NULL); /* this is a CPLEX callable library function */
I will get "invalid use of undefined type `struct Polytope'" error in compiling.
Right: Without "readLP.h", the compiler is ignorant of
the internals of a `struct Polytope'. From the code as shown
it might deduce that the struct has elements named `rhs' and
`sense' and so on, but it has no idea what these names mean.
It was working OK when all codes were in sigle file without using keyword "extern".
Presumably because the complete declaration of `struct
Polytope' was in that file, too. (But I suspect that the
`typedef' is a recent and probably incorrect addition.)
If I change main.h into: ... extern struct Polytope a_polytope, aa_polytope ; struct Polytope *pa_polytope, *paa_polytope ; ... and also change main.c into pa_polytope = &a_polytope ; status = CPXaddrows (env, lp, 0,num_rows, num_nonzero, pa_polytope->rhs,pa_polytop e->sense,pa_polyt ope->rmatbeg, pa_polytope->rmatind,pa_pol ytope->rmatval, NULL, NULL);
I will get "dereferenc ing pointer to incomplete type" four times for each pa_polytope->xxxx above.
Right: The compiler knows of two `struct Polytope'
variables ("Whatever *that* may be," says the compiler)
named `a_polytope' and `aa_polytope'. It also knows of
to pointers `pa_polytope' and `paa_polytope', defined
locally, that can be made to point to those structs (and
to others of the same type). But it's still without any
information about "the insides" of the struct; loosely
speaking, you've told it how to point "at" the struct as
a whole, but not how to "reach inside."
Does anyone have a suggestion?
Back to the C textbook, I think. There are enough
misunderstandin gs on exhibit here that you're probably
better off re-studying the fundamentals than trying to
blunder ahead in flat-Earthian ignorance -- you wouldn't
want to fall off the Edge, would you?
-- Er*********@sun .com
PCHOME wrote: Hello! I am working on dividing a single C file into several files. Now I encounter a problem about the global variables and can not find a way to solve it. All global variables and codes used to be in that single file, that worked OK. But when I divdie that file into several ones, I have many "invalid use of undefined type" errors.
The four files are main.c, main.h, readLP.h, and readLP.c.
In readLP.h I have:
#define MAX_NUMROWS 181010 #define MAX_NUMCOLS 201 #define MAX_NUMNZ 9000000 ...
typedef struct Polytope { int rmatbeg[MAX_NUMROWS]; int rmatind[MAX_NUMNZ]; double rmatval[MAX_NUMNZ]; double rhs[MAX_NUMROWS]; char sense[MAX_NUMROWS]; double cosine[MAX_NUMROWS]; double norm[MAX_NUMROWS]; /* char *rowname[MAX_NUMROWS]; */ } a_polytope, aa_polytope; ...
and in main.h, I have: ... extern struct Polytope a_polytope, aa_polytope ;
in main.c , if the external variable a_polytope is used as a function's parameters such as : status = CPXaddrows (env, lp, 0, num_rows, num_nonzero, a_polytope.rhs, a_polytope.sens e,a_polytope.rm atbeg, a_polytope.rmat ind, a_polytope.rmat val, NULL, NULL); /* this is a CPLEX callable library function */
I will get "invalid use of undefined type `struct Polytope'" error in compiling. It was working OK when all codes were in sigle file without using keyword "extern".
If I change main.h into: ... extern struct Polytope a_polytope, aa_polytope ; struct Polytope *pa_polytope, *paa_polytope ; ... and also change main.c into pa_polytope = &a_polytope ; status = CPXaddrows (env, lp, 0,num_rows, num_nonzero, pa_polytope->rhs,pa_polytop e->sense,pa_polyt ope->rmatbeg, pa_polytope->rmatind,pa_pol ytope->rmatval, NULL, NULL);
I will get "dereferenc ing pointer to incomplete type" four times for each pa_polytope->xxxx above.
Does anyone have a suggestion?
Thank you so much!
refer eric's reply to understand what you did wrongly.
now i'll tell you how can you correct this, i mean generally how its
done.
in readLP.h //remove typedef
/*typedef*/ struct Polytope {
int rmatbeg[MAX_NUMROWS];
int rmatind[MAX_NUMNZ];
double rmatval[MAX_NUMNZ];
double rhs[MAX_NUMROWS];
char sense[MAX_NUMROWS];
double cosine[MAX_NUMROWS];
double norm[MAX_NUMROWS];
/* char *rowname[MAX_NUMROWS]; */
} ;
extern struct Polytope a_polytope, aa_polytope;
in readLP.c
#include "readLP.h"
struct Polytope a_polytope, aa_polytope;
in any other file -------->
#include "readLp.h"
// optionally you can declare extern struct Polytope a_polytope,
aa_polytope;
//its your wish as it has already been done when you include readLP.h
rest is fine !!!
------------------
i love C
darius
Eric Sosman wrote: Presumably because the complete declaration of `struct Polytope' was in that file, too. (But I suspect that the `typedef' is a recent and probably incorrect addition.)
Yes. The `typedef' is a recent addition. I did not put it in my
orginalcodes. I added it desperately after dozens of trials. Does anyone have a suggestion?
Back to the C textbook, I think. There are enough misunderstandin gs on exhibit here that you're probably better off re-studying the fundamentals than trying to blunder ahead in flat-Earthian ignorance -- you wouldn't want to fall off the Edge, would you?
What C textbook will you suggest? I did look up at least 3
C-textbooks, but none of them mentions clearly about the topics of
"#include" and mutiple files project.
Thanks!
Darius wrote: extern struct Polytope a_polytope, aa_polytope;
in readLP.c #include "readLP.h" struct Polytope a_polytope, aa_polytope;
in any other file --------> #include "readLp.h" // optionally you can declare extern struct Polytope a_polytope, aa_polytope; //its your wish as it has already been done when you include readLP.h
rest is fine !!!
Do you mean that #include "readLp.h" is necessary in any other file
using struct Polytope a_polytope? but "extern struct Polytope
a_polytope" is optional?
Thanks!
Somehow, I do not know why I used to believe that putting the same '
#include "readLP.h" ' into more than 2 different c files would lead
mutiple definitions of variables. Now I know it is OK to do so.
Is it also OK to put 'int test_i = 0;' into that "readLP.h" and
include it in more than on files?
I might be wrong,but to me the 'int test_i = 0;' is a declaration puls
a definition.
I know it is OK to have 'int test_i;' in it becasue 'int test_i;' is
just a declararion. How about puting 'int test_i = 0' there? The
latter is a definition to me(I might be wrong here).
I looked up several C books before but could not find one clearly
describe topics about mutiple c files project, #incldue and other
preprocessor commands, and so on. Do you happen to know which C book
touch those topics in depth?
Thanks!
PCHOME wrote: Darius wrote:
extern struct Polytope a_polytope, aa_polytope;
in readLP.c #include "readLP.h" struct Polytope a_polytope, aa_polytope;
in any other file --------> #include "readLp.h" // optionally you can declare extern struct Polytope a_polytope, aa_polytope; //its your wish as it has already been done when you include
readLP.h rest is fine !!! Do you mean that #include "readLp.h" is necessary in any other
file using struct Polytope a_polytope? but "extern struct Polytope a_polytope" is optional?
yes even this code is valid
int func()
{
extern int a; // for some global variable a;
extern int a;
extern int a;
}
Thanks! Somehow, I do not know why I used to believe that putting the same
' #include "readLP.h" ' into more than 2 different c files would lead mutiple definitions of variables. Now I know it is OK to do so.
here is a catch; to save yourself from multiple definations
do this
<------------------------------readLP.h--------------------------------->
#ifndef PCHOME_READLP_H
#define PCHOME_READLP_H
<-----------here is all the code-------------->
#endif
you can use any identifier (a unique string) instead of READLP_H, in
this way
when you include that file first time PCHOME_READLP_H is defined and
next time becoz its already defined, the content is not included again.
you got it?
Is it also OK to put 'int test_i = 0;' into that "readLP.h" and include it in more than on files?
yes!!!, if you follow the rules above, else you'll get double
defination error
but i don't write any defination in .h files. just declare it using
'extern'
I might be wrong,but to me the 'int test_i = 0;' is a declaration
puls a definition. I know it is OK to have 'int test_i;' in it becasue 'int test_i;' is just a declararion. How about puting 'int test_i = 0' there? The latter is a definition to me(I might be wrong here).
I think its clear now. I looked up several C books before but could not find one clearly describe topics about mutiple c files project, #incldue and other preprocessor commands, and so on. Do you happen to know which C
book touch those topics in depth? Thanks!
kernighan & ritchie and C unleashed (rest you'll learn by experience)
---------------
darius This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Bryan Parkoff |
last post by:
….I would like to know which is the best optimization to use global
variable or global struct. I always tell C/C++ Compiler to turn on
optimization.
….I use underscore between first name and second name for better
readable. After optimization, global variables might be misaligned
because each global variables must be converted to 32 bits, but I do
see that C/C++ Compiler do padding between variables. Struct does the
same to do padding....
|
by: Andrej Prsa |
last post by:
Hi!
What happens to a globally defined pointer, e.g.
void *value;
that points to a particular type in a function:
int dummy_func (int a)
{
|
by: wilson |
last post by:
Hi All,
I am a novice at C and just have learned pointer for a short period.
Today one error had occured while executing my program below.
And I cannot find the error out since it's checked "OK" by Dev C++.
Here is:(I want to exchange the value of "a" and "b" with function "swap")
void swap(int *pa, int *pb)
{
|
by: Michael |
last post by:
Hi newsgroup,
as the subject indicates I am looking for an advice
using global variables. I am not if this problem is more
about style then C. If its wrong in thi group, sorry.
So I have a couple of function that all need the same
information (all located in the same file). By now it looks like
/* file beginns */
|
by: Sunil |
last post by:
Hi all,
I want to know how good or bad it is using global variables i.e
advantages
and disadvantages of using global variables.
Sunil.
| |
by: amit |
last post by:
Hello Group,
Does anybody know how I can have a global variable in an HTML file? for
instance, I have a fuction (called aFunction() here) and during a
mousedown or up event the function is going to be called.
The third passing arugment or parameter is myGlobarVar. How can I make
this work? since myGlobalVar is defined in a different block of
javascript within the HTML file?
|
by: Hakusa |
last post by:
I have the argument items in my class room.
class room:
def __init__(self, name, description, items*):
I thought I remembered from a tutorial I read once, and I've read so
many I feel like an expert of them, that putting a little star* above
an item makes it accept the argument as a list. But when I tried this,
I got an invalid syntax error message.
|
by: Lalatendu Das |
last post by:
I have seen a header file in which one structure is defined with
extern declaration in a header file and declared one variable of that
structure in one C-file. The code goes as mentioned below . I am
confused with the way it is declared and defined.
a.h
-------
:
:
|
by: matt |
last post by:
Hello group,
I'm trying to become familiar with the information hiding design rules, and I
have a lot (3) of questions for all you experts. AFAIK, a generic module has 2
files:
================
module.h
================
#ifndef __MODULE_HDR_INCLUDED__
|
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...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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,...
|
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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
|
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
| |
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...
| |