473,404 Members | 2,187 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,404 software developers and data experts.

question--separatecompilation

Dear All,

I have a question regarding to linking separate files for compilation.
For example, I have two file in the same directory: file1.cpp & file2.cpp.

==> file1.cpp <==
int iNumber= 999;

==> file2.cpp <==
#include <iostream>
using namespace std;

extern int iNumber;

int main()
{
cout << "iNumber= " << ::iNumber << endl;

return 0;
}

In file2, the variable iNumber cannot be seen. I thought file1.cpp

int iNumber=999;

makes the variable iNumber in file scope, and "extern int iNumber;"
statement tells file2.cpp that iNumber has been specified somewhere
else, therefore iNumber should be able to be printed out in file2.cpp.
But it cannot. What is missing? Is a "include" statement? How to add it?

Thank you very much.

Dec 14 '05 #1
18 1508
On 2005-12-14, Xiaoshen Li <xl**@gmu.edu> wrote:
Dear All,

I have a question regarding to linking separate files for compilation.
For example, I have two file in the same directory: file1.cpp & file2.cpp.

==> file1.cpp <==
int iNumber= 999;
It must be declared extern in file1.cpp, too. You would normally
use header files to make this easier.
==> file2.cpp <==
#include <iostream>
using namespace std;

extern int iNumber;

int main()
{
cout << "iNumber= " << ::iNumber << endl;

return 0;
}

In file2, the variable iNumber cannot be seen. I thought
file1.cpp


--
Neil Cerutti
Dec 14 '05 #2
Neil Cerutti wrote:
==> file1.cpp <==
int iNumber= 999;


It must be declared extern in file1.cpp, too. You would normally
use header files to make this easier.


Not at all. The declaration
int iNumber = 999
defines iNumber as a global variable, and hence it has external
linkage. It is available for the running program.

As for the OP, the programs compile and link well. Most porbably you
are not linking the two object files with each other
1) compile both files to get .o
2) link them

Things work fine on my machine.

Dec 14 '05 #3
Xiaoshen Li wrote:
I have a question regarding to linking separate files for compilation.
For example, I have two file in the same directory: file1.cpp & file2.cpp.

==> file1.cpp <==
int iNumber= 999;
extern int iNumber = 999;
==> file2.cpp <==
#include <iostream>
using namespace std;

extern int iNumber;

int main()
{
cout << "iNumber= " << ::iNumber << endl;

return 0;
}

In file2, the variable iNumber cannot be seen.
What does that mean? What error are you getting?
I thought file1.cpp

int iNumber=999;

makes the variable iNumber in file scope, and "extern int iNumber;"
statement tells file2.cpp that iNumber has been specified somewhere
else, therefore iNumber should be able to be printed out in file2.cpp.
But it cannot. What is missing? Is a "include" statement? How to add it?


Adding 'extern' to 'file1.cpp' should take care of that...

V
Dec 14 '05 #4


Neelesh Bodas wrote:
Neil Cerutti wrote:
==> file1.cpp <==
int iNumber= 999;


It must be declared extern in file1.cpp, too. You would normally
use header files to make this easier.

Not at all. The declaration
int iNumber = 999
defines iNumber as a global variable, and hence it has external
linkage. It is available for the running program.

As for the OP, the programs compile and link well. Most porbably you
are not linking the two object files with each other
1) compile both files to get .o
2) link them

Things work fine on my machine.

I believe you are correct. I think the problem is I don't know how to

1) compile both files to get .o
2) link them

I totally have no idea. I just did "gcc file2.cpp".

Any help would be greatly appreciated.

Dec 14 '05 #5
>> ==> file1.cpp <==
int iNumber= 999;
extern int iNumber = 999;


I thought global variables have by default external linkage. I don't
understand why extern is necessary.

Dec 14 '05 #6
> I totally have no idea. I just did "gcc file2.cpp".

Try gcc file1.cpp file2.cpp

Dec 14 '05 #7

Xiaoshen Li wrote:
I believe you are correct. I think the problem is I don't know how to

1) compile both files to get .o
2) link them

I totally have no idea. I just did "gcc file2.cpp".


<OT>
1) gcc -c file1.cpp
2) gcc -c file2.cpp
3) gcc file1.o file2.o
</OT>

Dec 14 '05 #8

Inevidently I deleted my previous post :-(

Victor Bazarov wrote:
Xiaoshen Li wrote:
I have a question regarding to linking separate files for compilation.
For example, I have two file in the same directory: file1.cpp & file2.cpp.

==> file1.cpp <==
int iNumber= 999;


extern int iNumber = 999;


Why do you need an exten here? global variables have external linkage
by default, don't they?

Dec 14 '05 #9
Neelesh Bodas wrote:
Inevidently I deleted my previous post :-(

Victor Bazarov wrote:
Xiaoshen Li wrote:
I have a question regarding to linking separate files for compilation.
For example, I have two file in the same directory: file1.cpp & file2.cpp.

==> file1.cpp <==
int iNumber= 999;


extern int iNumber = 999;

Why do you need an exten here? global variables have external linkage
by default, don't they?


Yes, they are. I just think that in some cases one should be explicit.
Especially if later somebody comes in and makes it 'const'...

V
Dec 14 '05 #10

I deleted one of my previous posts by clicking remove inevidently :-(

Victor Bazarov wrote:
Xiaoshen Li wrote:
I have a question regarding to linking separate files for compilation.
For example, I have two file in the same directory: file1.cpp & file2.cpp.

==> file1.cpp <==
int iNumber= 999;


extern int iNumber = 999;


Why do you need an extern here? isn't iNumber going to get external
linkage on defining it global?

My previous post said that the program works well on my machine when
the two object files are linked together.

Dec 14 '05 #11


Neelesh Bodas wrote:
<OT>
1) gcc -c file1.cpp
2) gcc -c file2.cpp
3) gcc file1.o file2.o
</OT>

I don't know why. In my Linux 7.3 RedHat machine, "gcc file1.o file2.o"
gave the error:gcc file1.o file2.o file2.o: In function `main':
file2.o(.text+0xa): undefined reference to `endl(ostream &)'
file2.o(.text+0x20): undefined reference to `cout'
file2.o(.text+0x25): undefined reference to `ostream::operator<<(char
const *)'
file2.o(.text+0x30): undefined reference to `ostream::operator<<(int)'
file2.o(.text+0x3b): undefined reference to `ostream::operator<<(ostream
&(*)(ostream &))'
collect2: ld returned 1 exit status

If "gcc file1.cpp file2.cpp", similar errors: gcc file1.cpp file2.cpp

/tmp/ccCwAXrZ.o: In function `main':
/tmp/ccCwAXrZ.o(.text+0xa): undefined reference to `endl(ostream &)'
/tmp/ccCwAXrZ.o(.text+0x20): undefined reference to `cout'
/tmp/ccCwAXrZ.o(.text+0x25): undefined reference to
`ostream::operator<<(char const *)'
/tmp/ccCwAXrZ.o(.text+0x30): undefined reference to
`ostream::operator<<(int)'
/tmp/ccCwAXrZ.o(.text+0x3b): undefined reference to
`ostream::operator<<(ostream &(*)(ostream &))'
collect2: ld returned 1 exit status

What is the problem with my machine? Thank you very much.

Again the two files are:
==> file1.cpp <==
int iNumber= 999;

==> file2.cpp <==
#include <iostream>
using namespace std;

extern int iNumber;

int main()
{
cout << "iNumber= " << ::iNumber << endl;

return 0;
}

Dec 14 '05 #12
Xiaoshen Li wrote:

I believe you are correct. I think the problem is I don't know how to

1) compile both files to get .o
2) link them

I totally have no idea. I just did "gcc file2.cpp".

Any help would be greatly appreciated.


This is off-topic for this newsgroup, as it concerns implementation
details. Try gnu.g++.help

Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Dec 14 '05 #13
On Wed, 14 Dec 2005 16:27:15 +0000, Xiaoshen Li wrote:


Neelesh Bodas wrote:
<OT>
1) gcc -c file1.cpp
2) gcc -c file2.cpp
3) gcc file1.o file2.o
</OT>


I don't know why. In my Linux 7.3 RedHat machine, "gcc file1.o file2.o"
gave the error:
>gcc file1.o file2.o

file2.o: In function `main':
file2.o(.text+0xa): undefined reference to `endl(ostream &)'
file2.o(.text+0x20): undefined reference to `cout'
file2.o(.text+0x25): undefined reference to `ostream::operator<<(char
const *)'
file2.o(.text+0x30): undefined reference to `ostream::operator<<(int)'
file2.o(.text+0x3b): undefined reference to `ostream::operator<<(ostream
&(*)(ostream &))'
collect2: ld returned 1 exit status

If "gcc file1.cpp file2.cpp", similar errors:
> gcc file1.cpp file2.cpp

/tmp/ccCwAXrZ.o: In function `main':
/tmp/ccCwAXrZ.o(.text+0xa): undefined reference to `endl(ostream &)'
/tmp/ccCwAXrZ.o(.text+0x20): undefined reference to `cout'
/tmp/ccCwAXrZ.o(.text+0x25): undefined reference to
`ostream::operator<<(char const *)'
/tmp/ccCwAXrZ.o(.text+0x30): undefined reference to
`ostream::operator<<(int)'
/tmp/ccCwAXrZ.o(.text+0x3b): undefined reference to
`ostream::operator<<(ostream &(*)(ostream &))'
collect2: ld returned 1 exit status


Use g++ instead of gcc.

- Jay
Dec 14 '05 #14
Victor Bazarov wrote:
Xiaoshen Li wrote:
==> file1.cpp <==
int iNumber= 999;
extern int iNumber = 999;


These two declarations have an identical effect.
Adding 'extern' to 'file1.cpp' should take care of that...


It makes no difference. The OP must have had some other
error (probably, failing to link both object files).

Dec 14 '05 #15

Xiaoshen Li wrote:
I don't know why. In my Linux 7.3 RedHat machine, "gcc file1.o file2.o"
gave the error:
>gcc file1.o file2.o

file2.o: In function `main':
file2.o(.text+0xa): undefined reference to `endl(ostream &)'
file2.o(.text+0x20): undefined reference to `cout'
file2.o(.text+0x25): undefined reference to `ostream::operator<<(char
const *)'
file2.o(.text+0x30): undefined reference to `ostream::operator<<(int)'
file2.o(.text+0x3b): undefined reference to `ostream::operator<<(ostream
&(*)(ostream &))'
collect2: ld returned 1 exit status

If "gcc file1.cpp file2.cpp", similar errors:
> gcc file1.cpp file2.cpp

/tmp/ccCwAXrZ.o: In function `main':
/tmp/ccCwAXrZ.o(.text+0xa): undefined reference to `endl(ostream &)'
/tmp/ccCwAXrZ.o(.text+0x20): undefined reference to `cout'
/tmp/ccCwAXrZ.o(.text+0x25): undefined reference to
`ostream::operator<<(char const *)'
/tmp/ccCwAXrZ.o(.text+0x30): undefined reference to
`ostream::operator<<(int)'
/tmp/ccCwAXrZ.o(.text+0x3b): undefined reference to
`ostream::operator<<(ostream &(*)(ostream &))'
collect2: ld returned 1 exit status

What is the problem with my machine? Thank you very much.


Error in my post. I meant g++ but accidently typed in gcc
Apologies.

Dec 15 '05 #16
On 2005-12-14, Neelesh Bodas <ne***********@gmail.com> wrote:
Neil Cerutti wrote:
>==> file1.cpp <==
> int iNumber= 999;


It must be declared extern in file1.cpp, too. You would normally
use header files to make this easier.


Not at all. The declaration
int iNumber = 999
defines iNumber as a global variable, and hence it has external
linkage. It is available for the running program.


Thanks for that. I did not realize what I was doing was merely a
convention!

--
Neil Cerutti
Dec 15 '05 #17


Xiaoshen Li wrote:
Dear All,

I have a question regarding to linking separate files for compilation.
For example, I have two file in the same directory: file1.cpp & file2.cpp.

==> file1.cpp <==
int iNumber= 999;

==> file2.cpp <==
#include <iostream>
using namespace std;

extern int iNumber;

int main()
{
cout << "iNumber= " << ::iNumber << endl;

return 0;
}

In file2, the variable iNumber cannot be seen. I thought file1.cpp

int iNumber=999;

makes the variable iNumber in file scope, and "extern int iNumber;"
statement tells file2.cpp that iNumber has been specified somewhere
else, therefore iNumber should be able to be printed out in file2.cpp.
But it cannot. What is missing? Is a "include" statement? How to add it?

Thank you very much.


I have solved the "problem" by other news group. Just by:
g++ file1.cpp file2.cpp

Then
../a.out
Dec 15 '05 #18


==> file1.h <==
extern int iNumber;

==> file1.cpp <==
#include "file1.h"
int iNumber= 999;

==> file2.cpp <==
#include <iostream>
using namespace std;
#include "file1.h"

int main()
{
cout << "iNumber= " << ::iNumber << endl;
return 0;
}
Then the compiler will be able to verify that your definition in
file1.cpp matches the declaration in file1.h, and that your use in
file2.cpp matches the declaration in file1.h too.


For better organizing files, can I put file1.h in a header files
directory and *.cpp files in source file directory? Say
/home/header_file/file1.h
/home/source_file/file1.cpp
/home/source_file/file2.cpp

In such condition, what should I change to the 3 files above in oder to
let file2.cpp be compiled and run?

Thank you very much.
Dec 15 '05 #19

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

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.