OSDev.org

The Place to Start for Operating System Developers
It is currently Sat May 04, 2024 12:07 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 67 posts ]  Go to page Previous  1, 2, 3, 4, 5  Next
Author Message
 Post subject: Re:a c question
PostPosted: Thu Jan 15, 2004 3:35 pm 
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.


Top
  
 
 Post subject: Re:a c question
PostPosted: Thu Jan 15, 2004 7:01 pm 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 9:01 am
Posts: 842
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


Top
 Profile  
 
 Post subject: Re:a c question
PostPosted: Fri Jan 16, 2004 3:19 am 
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.


Top
  
 
 Post subject: Re:a c question
PostPosted: Fri Jan 16, 2004 11:19 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 9:01 am
Posts: 842
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?

_________________
Only Human


Top
 Profile  
 
 Post subject: Re:a c question
PostPosted: Fri Jan 16, 2004 12:34 pm 
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:
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:
gcc a.c
gcc super.c
ld a.o super.o


Top
  
 
 Post subject: Re:a c question
PostPosted: Sat Jan 17, 2004 8:18 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 9:01 am
Posts: 842
@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:
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?

_________________
Only Human


Top
 Profile  
 
 Post subject: Re:a c question
PostPosted: Sat Jan 17, 2004 10:02 am 
use 'ar' to do this.


Top
  
 
 Post subject: Re:a c question
PostPosted: Sat Jan 17, 2004 11:23 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 9:01 am
Posts: 842
isn;t ar used to create an archieve file wont this file format be different from a object file?

_________________
Only Human


Top
 Profile  
 
 Post subject: Re:a c question
PostPosted: Sat Jan 17, 2004 2:24 pm 
Yes, but ld recognises both formats.
Code:
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


Top
  
 
 Post subject: Re:a c question
PostPosted: Sat Jan 17, 2004 2:25 pm 
Offline
Member
Member
User avatar

Joined: Tue Oct 17, 2006 11:33 pm
Posts: 3882
Location: Eindhoven
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:
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:
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:
ld -r -o $i.o <all .o-files in a list>


you can finish the link:
Code:
gcc -o $i <all .o files you have left>


and you can make a static archive from it:
Code:
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:
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


Top
 Profile  
 
 Post subject: Re:a c question
PostPosted: Wed Jan 21, 2004 11:18 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 9:01 am
Posts: 842
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


Top
 Profile  
 
 Post subject: Re:a c question
PostPosted: Wed Jan 21, 2004 1:22 pm 
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.


Top
  
 
 Post subject: Re:a c question
PostPosted: Wed Feb 04, 2004 1:02 pm 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 9:01 am
Posts: 842
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


Top
 Profile  
 
 Post subject: Re:a c question
PostPosted: Wed Feb 04, 2004 1:57 pm 
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.)


Top
  
 
 Post subject: Re:a c question
PostPosted: Sat Feb 07, 2004 11:07 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 9:01 am
Posts: 842
why doesn't this work
Code:
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:
&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?)

_________________
Only Human


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 67 posts ]  Go to page Previous  1, 2, 3, 4, 5  Next

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group