ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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"?
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??
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).
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");
.......
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.