a c question
Re:a c question
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
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?
Only Human
Re:a c question
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.
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
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?
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?
Only Human
Re:a c question
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:
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)
So if you're using just gcc:
Code: Select all
gcc a.c super.c
Code: Select all
gcc a.c
gcc super.c
ld a.o super.o
Re:a c question
@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
but gcc tells me that -o option does not work with multiple files
is there any other way to do this?
i tried using
Code: Select all
gcc -c super.c a.c -o combi.o
is there any other way to do this?
Only Human
Re:a c question
isn;t ar used to create an archieve file wont this file format be different from a object file?
Only Human
Re:a c question
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
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: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 usingbut gcc tells me that -o option does not work with multiple filesCode: Select all
gcc -c super.c a.c -o combi.o
is there any other way to do this?
Code: Select all
gcc -c -o $i.o $i.c
Code: Select all
ld -r -o $i.o <all .o-files in a list>
Code: Select all
gcc -o $i <all .o files you have left>
Code: Select all
ar -rcs file.a <all .o files you want>
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
Re:a c question
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?
Only Human
Re:a c question
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
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?
Only Human
Re:a c question
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
why doesn't this work
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
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?)
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);
}
Code: Select all
&tim2.hr, &tim2.min, &tim2.sec
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?)
Only Human