LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-04-2009, 04:39 AM   #1
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
confused with compile result


Code:
int myinteger;

int myfunction();

int main(int argc, char *argv[])
{

        myinteger=33;

        myfunction();

        printf("myinteger is now %d\n",myinteger);
        return 0;


}
Code:
#include <stdio.h>

extern int myinteger;

int myfunction()
{
        printf("Hallo;\n myinteger is %d\n",myinteger);
        myinteger=44;
        return 0;
}
when I compile this code with
Code:
gcc -Wall test1.c test2.c -o myapp
it is happy

If I omit the extern and recompile, it's still happy? Question is why that would be From my turboC days I seem to remember that 'extern' was required in situations like above. Does the compiler nowadays take care of this? Or is there something else that I'm missing?
 
Old 08-04-2009, 04:59 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Wim Sturkenboom View Post
Code:
int myinteger;

int myfunction();

int main(int argc, char *argv[])
{

        myinteger=33;

        myfunction();

        printf("myinteger is now %d\n",myinteger);
        return 0;


}
Code:
#include <stdio.h>

extern int myinteger;

int myfunction()
{
        printf("Hallo;\n myinteger is %d\n",myinteger);
        myinteger=44;
        return 0;
}
when I compile this code with
Code:
gcc -Wall test1.c test2.c -o myapp
it is happy

If I omit the extern and recompile, it's still happy? Question is why that would be From my turboC days I seem to remember that 'extern' was required in situations like above. Does the compiler nowadays take care of this? Or is there something else that I'm missing?
Why shouldn't the compiler be happy ?

Without the "extern" you declare your global 'myinteger' in the same file as 'myfunction', so what ? Inside 'myfunction' is known the first time it is used.

The linker might be unhappy because it sees two global entities with the same name, but probably you need to supply linker with a command line flag that would enable such warnings.

...

Maybe you are wondering about a good way to declare/initialize global variables. If it's the case I've found a solution which suits me well. Let me know if you are interested.
 
Old 08-04-2009, 06:09 AM   #3
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Original Poster
Rep: Reputation: 282Reputation: 282Reputation: 282
Sorry, my mistake, it should have read linker.

Further I would expect running the program without 'extern' should give me an uninitialized 'myinteger' in the second file and therefore random results.

Always interested in solutions that others have found. My usual solution is to have an define __SOMETHING__ in my 'main' source file and not in the other source files and the include file will contain ifdef __SOMETHING__ to determine if a variable must be declared as external or normal.

The code posted earlier was a quick and dirty example that I gave to someone and I was surprised that it compiled/linked/worked correctly in both cases.

WimS
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
join result tostay2003 Programming 8 08-14-2008 07:18 PM
no result after compile, nasim751 Programming 1 01-29-2008 03:41 PM
Confused nay Very Confused chrystlenight SUSE / openSUSE 3 08-28-2007 05:57 PM
rpmbuild where is the result? JaveChen Fedora 5 06-25-2006 07:58 PM
df gets different result from du? liyuefu Linux - General 4 10-01-2005 10:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration