Menu

Confused on a Concept

2009-02-26
2012-09-26
  • Brandon Branston

    I just started getting into the Allegro library and got it working with little issues, but I am the kind of person who likes to know enough about the background to have a feel of everything that is happening. My question is what exactly does a linker do, or what does it contain? in order for me to use the allegro library after installation, I've been needing to add a "linker" to the project settings (either adding -lalleg as a linker, or just specifying the specific header). I have previously understood that Dev C++ would automatically be able to access various headers by searching the default directories of such files, and I would be able to use them after moving such files to these locations, but this is not the case, which is why I must be missing something.

    In a nut shell, if Dev C++ has default directories to search for the headers I need, why must i explicitly add them through my project settings? What is this linker for?

    If it's not much to ask, LOTS of detail would be greatly appreciated.

    Thanks so much! =D

    Twinfun

    PS. Google has failed me...

     
    • cpns

      cpns - 2009-02-26

      > either adding -lalleg as a linker

      Actually that adds a library (liballeg.a). The linker is the tool that links object code from the library with object code generated from your source code by the compiler. A library is a collection of pre-compiled code.

      > or just specifying the specific header

      A header file is just a source code file that is inserted into to the code at the point of inclusion. Simple as that. However because a header file may be included by more than one compiled source file, you have to avoid putting code that must be defined only once in a single executable. So no instantive code, just declarations, prototypes, inline code, and templates. A header on its own is not a library, it merely provides information the compiler needs to use library code.

      > and I would be able to use them after moving such files to these locations

      Not the best idea. I prefer to keep the libraries supplied with the compiler separate from third-party libraries. It makes reinstallation or moving projects to new machines far easier. It is generally better to add library search paths. Look in the log, the paths searched are those specified by the -L<path> options. You can add more to your project options, or for general purpose stuff, you may want to add then to the general compiler options (which will only affect newly created projects).

      > if Dev C++ has default directories to search for the headers I need,
      > why must i explicitly add them through my project settings?

      The -L<path> options merely tell the linker where to look for library files. It does not tell it which libraries to actually search to resolve symbols. If the linker searched all libraries in the default library folder (and any others specified), then the build process would take much much longer! Moreover, some libraries may contain identical symbols, and unless you are explicit about which library you need, you may end up linking the wrong code.

      I described the whole build process and toolchain components a few years ago, and that may assist you: https://sourceforge.net/forum/message.php?msg_id=2670009

      Clifford

       
    • Wayne Keen

      Wayne Keen - 2009-02-26

      In the thread titled "Please Read Before Posting a Question" is a section on the compile log, including headers, and limking libraries that may get you started.

      Wayne

       
    • Wayne Keen

      Wayne Keen - 2009-02-26

      Keep in mind by the way that you are dealing with two beasts here:

      (1) The header. In MOST* cases, it is just a menu. It tells you what functions are available from Allegro. It does not provide any code to back these promises up. When you include a header, this menu is actually brought into your code, enabling the compiler to check if you are calling functions it knows about.

      (2) The library. This is where the actual compiled code is that does the things that the header promises. For your code to actually get hold of this code, it must be linked with it. This is where the linker comes in.

      Note that when linking code in, you only want to link the specific library you need. That is where the -lxyz command comes in.

      Note that this linking prcoess is also how code contained in the multiple files of a project are brought together.

      Wayne

      *there are some special cases where headers do have code in them, but that is rare at this leveel

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.