Reputation: 848
I am working on a homework assignment to implement different page replacement algorithms for my operating systems class. A problem that I am running into is that I want to implement each algorithm in a different C file, and within each individual file, use the same function names like search()
, insert()
, updatePageTable()
, etc.
However, when I compile the files, the compiler complains about duplicate symbols. Is there a way to just import the main test function from each different algorithm file and have those functions reference their file's functions?
Upvotes: 4
Views: 371
Reputation: 753805
Transferring comments 1 and 2 into an answer.
You can hide functions within a file by declaring them static
; this is good practice. You will have at least one function that is not so hidden (sometimes that'll be main()
, sometimes it'll be some other function). If the non-static function is not main()
, then you should have a header that declares that function. The header should be used where the function is implemented to ensure that the definition matches the declaration, and the header should be used where the function is used to ensure that it is used correctly.
Note that a given program can include just one externally visible definition of any given function name. That is, there can be at most one file that defines a global (externally visible) function called insert()
, for example, but there may be lots of files that define a static function called insert()
— but those files cannot reference, or even have declared, the global function. A given source file can either access the global function insert()
or it can access its own static function insert()
, but not both.
Upvotes: 2