Friday, July 31, 2009

Question on C functions programming language?

( you can see my previous question or read it here again)


I have declared various functions outside the main() function. all the functions can be called from main(), but some of the functions are better called from the other functions defined outside main(), otherwise I have to allocate another memory. Someone suggested I creat a lib file of the functions that I need to acess...how can I creat a lib file. Sorry if i sound stupid, but I have never created a lib file myself. I am using visual C++, but will be using CodeComposer, soon. But I have to simulate everything on pc, any help....?

Question on C functions programming language?
Do you have access to the command line? I am not a visual C++ guy, I do all my stuff via Makefiles on UNIX systems. Can you access the "ar" command in your compiler/linker?





If so, here is a simple command to build a static library out of several compiled ".o" (object) files.





ar rsuv %26lt;library name%26gt;.a %26lt;1st file%26gt;.o %26lt;2nd file%26gt;.o





Where:


---------


"ar" is the command


"rsuv" are command line switches that you can learn about by looking at your "man pages" for the "ar" command


"library name" is the name of the library file you are creating, and will have a ".a" suffix to denote that it is a static library


"yourfile" 1 and 2 are the object files that were created during your compilation step.





Voila - once you have this library, it can be linked in using the "ld" command with other libraries to create your executable program.





Sorry if this is useless to you since you might only be familiar with graphical programming. Perhaps it might be useful to others if not for you.





Good luck!
Reply:Your question isn't very clear. You're saying you need to call a function from a function...that doesn't have anything to do with lib files. All you need to do is make sure that the function you're calling is declared previously (i.e. before the function you're currently in). The only way a lib file comes in is if you need to keep the functions completely seperated from your main cpp/h file. In that case you would just create a seperate cpp/h file and compile it, but don't link it. Then you would declare those functions as "extern" in your main cpp/h file and make sure to inclue your lib file in the link parameters of your main program (of course you would have to compile the functions cpp/h first to get the lib file). Just be ready for a lot of confusion dealing with extern though, because it's not easy the first time you're doing it.


No comments:

Post a Comment