Jump to content
  • Advertisement
Sign in to follow this  
lordcorm

How do links work?

This topic is 4387 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In C++ how do links work do you just add them to the compile command line or do u have to do something else and would i add a health and medicine system that i wrote to a game by linking?

Share this post


Link to post
Share on other sites
Advertisement
I'm not quite sure what you're asking -- are you asking about the linker?

As you may have noticed when compiling programs, your compiler generates some intermediary files called object files that probably have file extensions like .o or .obj. These are binary files that are much more machine-readable than the C++ source code files you write, but they're still not the finished product. Generally speaking, a compiler will work on each .cpp source file independently and (provided there were no errors in doing so) it will generate from the source file an output object file. At that stage the compiler is done, and the linker will take over. The linker takes these object files and links them together into a final output executable file. In this manner, you can work on a single project that is composed of many source files, and still manage to combine them together into a single program.

There are several advantages to this strategy of organization. If you're using an intelligent make system, then modifying a single source file will only require that you recompile that single file (instead of all of the source files in the project, since files that haven't changed would just generate the same object files that are already there) and relink the object files in order to build your program. Also, you can decide to build a library instead of an executable as your output, and libraries can in turn be linked directly in with the object files of other programs. This is how you are hooking in standard library calls like for printing and working with files -- these calls aren't syntactically speaking a part of the language itself; rather, they're amongst a set of standard libraries that are included in standard distributions of C++ compilers.

Your intuition about adding complicated systems (like a health or medicine system, sure) to a program via the linker is correct -- you would want to write code in separate source files, keeping things organized, and compile separate object files, and link things together to complete the build process.

Here is a wikipedia article for more information: http://en.wikipedia.org/wiki/Linker

As for how to actually get things linking at the command line, it varies by compiler. I recommend that you read through your compiler documentation or ask around (asking about a specific C++ compiler) to find your answer.

Share this post


Link to post
Share on other sites
What i meen is this:
If i created to differnt files (mainframe and monster) how whould i make it so that i can link the monster file to the mainframe and get the monster function and use it in the mainframe

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
in the file you want to use the functions in you would put
#include "filename.h"

this gives you access to classes and functions in other files.

Share this post


Link to post
Share on other sites
Quote:
Original post by Bucket_Head
I'm not quite sure what you're asking -- are you asking about the linker?

As you may have noticed when compiling programs, your compiler generates some intermediary files called object files that probably have file extensions like .o or .obj. These are binary files that are much more machine-readable than the C++ source code files you write, but they're still not the finished product. Generally speaking, a compiler will work on each .cpp source file independently and (provided there were no errors in doing so) it will generate from the source file an output object file. At that stage the compiler is done, and the linker will take over. The linker takes these object files and links them together into a final output executable file. In this manner, you can work on a single project that is composed of many source files, and still manage to combine them together into a single program.

There are several advantages to this strategy of organization. If you're using an intelligent make system, then modifying a single source file will only require that you recompile that single file (instead of all of the source files in the project, since files that haven't changed would just generate the same object files that are already there) and relink the object files in order to build your program. Also, you can decide to build a library instead of an executable as your output, and libraries can in turn be linked directly in with the object files of other programs. This is how you are hooking in standard library calls like for printing and working with files -- these calls aren't syntactically speaking a part of the language itself; rather, they're amongst a set of standard libraries that are included in standard distributions of C++ compilers.

Your intuition about adding complicated systems (like a health or medicine system, sure) to a program via the linker is correct -- you would want to write code in separate source files, keeping things organized, and compile separate object files, and link things together to complete the build process.

Here is a wikipedia article for more information: http://en.wikipedia.org/wiki/Linker

As for how to actually get things linking at the command line, it varies by compiler. I recommend that you read through your compiler documentation or ask around (asking about a specific C++ compiler) to find your answer.




just want to let u know that wasnt a waste of time writing all that for him, cuz i read it and learned from it =)

Share this post


Link to post
Share on other sites
Quote:
Original post by lordcorm
What i meen is this:
If i created to differnt files (mainframe and monster) how whould i make it so that i can link the monster file to the mainframe and get the monster function and use it in the mainframe


Ah okay. Well, for this, we need to remember a few things: mainly, we need to be able to differentiate between definitions and declarations, and we need to remember that each source file is compiled from scratch separately.

For the first part, a definition is actually where you specify that you want to reserve a spot for something to be created definitely, whereas a declaration merely indicates that the thing is defined somewhere, without actually defining the thing. (You don't necessarily need both; a definition by itself works fine as a declaration, too). You may wonder why you need this distinction, but it does come in handy in many cases. If you're defining two things (say, two functions) that each need to call the other, you may be in trouble, since the definition of one necessarily has to go before the other. Before the compiler has read through the definition of the second, it won't know what you're talking about when you try to call it from the first, and so will generate a compiler error -- an "unidentified identifier." However, if you simply add a declaration of the second function up above the definition of the first function, then the compiler will have an idea of what you're talking about when you try calling the as-yet-to-be-defined function, and things will go smoothly.

What I mean by the second part is, when the compiler compiles each of your .cpp files, it does so without being aware in the slightest than any of the others exist. This is why you need to have the same set of #includes or whatever at the top of each one -- the slate is wiped clean with each new file, so every new thing you want to work with needs to be redeclared.

Now we can start to see how declarations allow you to lay things out in different files, but so that they can still reference each other. Just make sure that when the compiler compiles a file, everything that's referenced has either been defined or declared within "sight" of the file (either in it itself or via #includes) so that all identifiers will be understood. The other thing that's required is that whatever's being referenced (be it a function, global variable instance, or whatever) can be resolved in the linking stage -- if you declare something that isn't actually defined anywhere, you'll compile just fine, but linking will fail. Likewise, if you define it in more than one object, when you try to link, you'll get errors due to redundant definitions.

The last thing I'll comment is a matter of style and code hygiene. Suppose you have a function defined in one source file that you reference in a dozen other source files. You could copy and paste the function prototype (which serves to declare the function's existence) into each of these source files, and that would work fine. However, if you were to then, say, add another argument to the function's argument list, and forget to change all of the prototypes, you'd get linking errors due to things not matching up -- and you'd manually have to look through and find the problem and fix it. That can be a hassle!

A better solution is to put the declaraction in a single header file, and have all of the source files that want to reference it #include that. This way you only need to make the change in one place, and then the compiler will be able to point out all the erroneous spots where you forgot to add the extra argument where you made the call, complete with line numbers -- which is much better than the linker will do, which is basically just tell you "oh, linking error...this symbol is undefined in this object. Don't look at me; you wrote it."

Quote:
Original post by Oggan
just want to let u know that wasnt a waste of time writing all that for him, cuz i read it and learned from it =)


Glad you found it worthwhile.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!