LinuxQuestions.org
Help answer threads with 0 replies.
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-20-2005, 08:47 AM   #1
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Rep: Reputation: 45
compile many c progrs


Hi how i can compile many c files?
I have main.c 1.c 2.c 1.h 2.c
I have tried sth like
gcc -o main main.c 1.c 2.c

1.c includes 1.h
2.c includes 2.h
 
Old 08-20-2005, 09:14 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
"gcc -o main main.c 1.c 2.c" looks OK, and should work (that is, if your code is correct). What errors do you get?

Try compiling with:
Code:
gcc -Wall -pedantic -o main main.c 1.c 2.c
Then will get all warnings from the compiler, which may give you clue about what's wrong.

Last edited by Hko; 08-20-2005 at 09:15 AM.
 
Old 08-20-2005, 02:22 PM   #3
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Re: compile many c progrs

Quote:
Originally posted by alaios
Hi how i can compile many c files?
I have main.c 1.c 2.c 1.h 2.c
I have tried sth like
gcc -o main main.c 1.c 2.c

1.c includes 1.h
2.c includes 2.h
Tell us exactly what you got when you ran the gcc command line. On the face of it, it looks like it should have worked, so if we can see what you saw we can help you without having to make random guesses.

How are you including the header file? <1.h>, or "1.h"?
 
Old 08-21-2005, 04:12 AM   #4
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
gcc -DINET6 -O2 -march=i586 -mcpu=i686 -fmessage-length=0 -Wall -Wall -fno-strict-aliasing -Dlinux -I/usr/include/rpm -I. -I/usr/include -L/usr/lib -lnetsnmp -lcrypto -lm -ldl -lcrypto -lm -ldl -D_POSIX_C_SOURCE -lpthread -g -ggdb -o 3threads 3threads.c monitor.c
 
Old 08-21-2005, 04:42 AM   #5
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
Thx a lot

After searching a little i think i know whats wrong...
I have 2 files
main.c
1.c
1.h


1.h contains
#include statements
#def statements
#functions declarations
#variables declarations

1.c contains
includes the header file 1.h
functions that handle the variables declared in 1.h


main.c wants to
-initialize the variables declared in 1.h file
-use the functions that were declared in 1.h and implemented in 1.c

I think that my code organization is now fine... plz give me your comments
How can I compile this programme and how can I declare the functions for being used from main.c (extern)?

Thx a lot for your answers....
P.s When i declare some variables in a header file (Is that correct?) Can i initialize them also in the header file??


Have a nice day
 
Old 08-21-2005, 07:54 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
When i declare some variables in a header file (Is that correct?) Can i initialize them also in the header file??
You can, but you shouldn't, declare variables in header files.
(except special cases maybe, but it's better to leave those to specialists (which doesn't include me BTW)).

Header files should not contain code (like declarations), but only definitions. In practice, headers should only:
  • #include other header files, but as few as possible: only headers that are needed by the header itself.
  • #define macro's.
  • typedef's and structure definitions.
  • Prototype of functions, but only those that may be called by other .c files.
(Disclaimer: This list may not be 100.0% complete).
 
Old 08-21-2005, 01:39 PM   #7
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
Thx but if i want to declare some variables that never wont change why should I put them in the header file?
 
Old 08-21-2005, 04:05 PM   #8
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by alaios
Thx but if i want to declare some variables that never wont change why should I put them in the header file?
Nothing wrong with that.
However, in C, you'd usually #define a macro for constants, instead of declaring a constant variable.
 
Old 08-22-2005, 02:40 AM   #9
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
Thx but how i can define these in to a header file?
char available_sections[10][20];
strcpy(available_sections[0],"section1");
strcpy(available_sections[1],"section2");
.......
 
Old 08-22-2005, 04:48 AM   #10
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
IMHO those belong in a .c file.
 
Old 08-22-2005, 04:55 AM   #11
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
but these values would never change....
 
Old 08-22-2005, 05:59 AM   #12
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
You don't call functions in header files.

why not do something like:

Code:
#define NR_SECTIONS 3

char * available_sections[NR_SECTIONS] = {
                                "section1",
                                "section2",
                                "section3" };
 
Old 08-22-2005, 08:39 AM   #13
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by alaios
but these values would never change....
Headers files are meant to be #included in (multiple) .c files (and maybe other .h files).

Each source-file that #includes your header, will create its own instance of your vailable_sections[][] array. And each will also run strcpy() to fill it. All with the same values.

If you #include your header 6 times (and headers are meant to be #included in multiple source-files), you program will have 6 entire copy's of your array. While you need only one. This is waste of memory and CPU.

I suspect also that source files that #include your header, do need to access the array (is that correct?). In that case they shouldn't have access to it either, which means it doesn't belong in a header.
 
  


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
compile .c p4inkill4 Linux - Newbie 4 10-28-2005 03:26 PM
What to Compile Next? AxXium Slackware 6 07-06-2005 09:32 AM
ntop compile/post-compile problem? tjb Linux - Software 3 12-28-2004 04:22 PM
Why can't I compile anything? Vlad_M Linux - Software 5 04-11-2003 08:56 AM
Nothing will compile... SoulSe Linux - Software 6 04-07-2003 12:26 PM

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

All times are GMT -5. The time now is 04:36 AM.

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