Page 2 of 5

Re:a c question

Posted: Thu Jan 15, 2004 3:35 pm
by Tim
Then no. static functions and variables can only be accessed from the same source file in which they're declared. There's not a lot of point putting static declarations into a header file.

Re:a c question

Posted: Thu Jan 15, 2004 7:01 pm
by Neo
tim: it didnt work.the functions in super.h which were implemented in super.c could not be called in a.c by including super.h. i got an undefined function error.any ideas?

Re:a c question

Posted: Fri Jan 16, 2004 3:19 am
by Tim
Where did you get the error from -- the compiler or the linker?

If it's the compiler, then you haven't declared the function properly in super.h.

If it's the linker, then the function isn't in super.c, or you haven't included super.o in the linker command line, or you've made the function static. Remember static functions can't be seen outside the C file where they're implemented.

Re:a c question

Posted: Fri Jan 16, 2004 11:19 am
by Neo
the error was from the linker
heres what i did
1) created a file super.h with defintion of a function testp();
2) created a file super.c which implemented the function testp();
3) created a a.h and a.c
4) included super.h in a.h
5) included a.h in a.c and called testp() from main()
6) used gcc a.c
then i got a linker error undefined reference to _testp()
what should i do?

Re:a c question

Posted: Fri Jan 16, 2004 12:34 pm
by Tim
gcc isn't clever enough to pull in super.c when you include super.h. You have to tell it the names of all your source files.

So if you're using just gcc:

Code: Select all

gcc a.c super.c
Or if you're using both the compiler and the linker so that you don't have to compile all the files all the time: (better for big projects)

Code: Select all

gcc a.c
gcc super.c
ld a.o super.o

Re:a c question

Posted: Sat Jan 17, 2004 8:18 am
by Neo
@Tim: what i basically want to do is get 2 files (say a.c and super.c) to output a single object file not executable file. this will enable me to use the single object file instead of a long list of files each time.
i tried using

Code: Select all

gcc -c super.c a.c -o combi.o
but gcc tells me that -o option does not work with multiple files
is there any other way to do this?

Re:a c question

Posted: Sat Jan 17, 2004 10:02 am
by BI lazy
use 'ar' to do this.

Re:a c question

Posted: Sat Jan 17, 2004 11:23 am
by Neo
isn;t ar used to create an archieve file wont this file format be different from a object file?

Re:a c question

Posted: Sat Jan 17, 2004 2:24 pm
by Tim
Yes, but ld recognises both formats.

Code: Select all

gcc -c super.c -o super.o
gcc -c a.c -o a.o
ar rcs lib.a super.o a.o
ld -o whatever some_more.o lib.a

Re:a c question

Posted: Sat Jan 17, 2004 2:25 pm
by Candy
Neo wrote: @Tim: what i basically want to do is get 2 files (say a.c and super.c) to output a single object file not executable file. this will enable me to use the single object file instead of a long list of files each time.
i tried using

Code: Select all

gcc -c super.c a.c -o combi.o
but gcc tells me that -o option does not work with multiple files
is there any other way to do this?
Well, there are some things you can do, and some complex things composed of these simple things. For instance, you can compile a single source file:

Code: Select all

gcc -c -o $i.o $i.c
$i stands for the name of the file (BASH script-ish). You can combine some .o files into a single .o file (incremental linking):

Code: Select all

ld -r -o $i.o <all .o-files in a list>
you can finish the link:

Code: Select all

gcc -o $i <all .o files you have left>
and you can make a static archive from it:

Code: Select all

ar -rcs file.a <all .o files you want>
So, if you want 2 .c files to yield 1 .o file, compile each separately and then link them together:

Code: Select all

gcc -c -o super.o super.c
gcc -c -o a.o a.c
ld -r -o all.o a.o super.o
HTH, Candy

Re:a c question

Posted: Wed Jan 21, 2004 11:18 am
by Neo
Another C question. I have a function which accepts a float/double as argument in a separate file. When i tried calling this function from a different file i kept getting only zeros as the output. I finally found out that the caller was passing the arg as an int because the function was not explicitly declared in the calling file (i was compiling separately and linking them so i didn't #include the file). I did this and it worked fine. I just wanted to know if this has to be done or is there any other way around this?

Re:a c question

Posted: Wed Jan 21, 2004 1:22 pm
by Schol-R-LEA
The 'way around it' is to include the header. Unlike [tt]import[/tt] in Java or Modula-2, or [tt]use[/tt] in Ada and Object Pascal, C and C++ header includes don't have anything to do with linking; they are just straight-out text insertions. See reply #3 in this thread, reply #13 in this thread, and reply #10 in thread (discusses NASM's %include directive, but it works the same way) for more details.

Re:a c question

Posted: Wed Feb 04, 2004 1:02 pm
by Neo
is the main() function the only starting point of a C application program or can this be changed to some other function say 'mystart()'. If so how do i do this?

Re:a c question

Posted: Wed Feb 04, 2004 1:57 pm
by Tim
By the C standard, main is always the entry point. Your compiler and linker may allow you to change that. However, none of the compilers I know of let you rename main. (MS LINK and ld let you specify a completely different entry point, but if you do that you need to call the C runtime startup code yourself.)

Re:a c question

Posted: Sat Feb 07, 2004 11:07 am
by Neo
why doesn't this work

Code: Select all

typedef struct{
   char    sec;
   char   min;
   char   hr;
   int    day;
} time;

time addtime(time t1,time t2)
{
   time op;
   op.min=0;
   op.hr=0;

   op.sec = t1.sec + t2.sec;
   if(op.sec>59){
       op.sec %= 60;
      op.min++;
   }

   op.min += t1.min + t2.min;
   if(op.min>59){
      op.min %= 60;
      op.hr++;
   }

   op.hr += t1.hr + t2.hr;
   return op;
}

main()
{
 time op,tim1,tim2;
 tim1.sec=40; tim1.min=15; tim1.hr=11;
 scanf("%d%d%d",&tim2.hr,&tim2.min,&tim2.sec);   // THIS LINE
 printf("\nTime2= %d:%d:%d",tim2.hr,tim2.min,tim2.sec);
 op=addtime(tim1,tim2);
 printf("\n%d:%d:%d",op.hr,op.min,op.sec);
}
In the line indicated above (scanf()) there seems to be something wrong it does not get the 2nd 2 values and gives 0,0,<correct 3rd val> on printing. I thought maybe there was something wrong in getting the values using

Code: Select all

&tim2.hr, &tim2.min, &tim2.sec
in the scanf line, so i tried different forms such as &(tim2).hr etc... but nothing changes.
So what is the actual way to read in structure member values. (Direct assignment works but then do i have to get the values in a separate variable and then store them in the structure variable members?)