Say I define a function in the file func1.c
, and I want to call it from the file call.c
,
how would I accomplish this? Thanks in advance!
相关问题
- Multiple sockets for clients to connect to
- Keeping track of variable instances
- What is the best way to do a search in a large fil
- How to get the maximum of more than 2 numbers in V
- glDrawElements only draws half a quad
Use a Forward Declaration
For example:
There is a feature that allows you to reuse these forward declarations called Header Files. Simply take the forward declarations, place them in the header file, then use
#include
to add them to each C source file you reference the forward declarations in.Of course, to be able to compile both source files, and therefore the entire library or executable program, you'll need to add the output of both .c files to the linker command line, or include them in the same "project" (depending on your IDE/compiler).
Many people suggest that you make header files for all your forward declarations, even if you don't think you'll need them. When you (or other people) go to modify your code, and change the signature of functions, it will save them time from having to modify all of the places where the function is forward-declared. It may also help save you from some subtle bugs, or at least confusing compiler errors.
You would put a declaration for the function in the file
func1.h
, and add#include "func1.h"
incall.c
. Then you would compile or linkfunc1.c
andcall.c
together (details depend on which C system).