Go to Post It is one thing to be a great designer, it is another thing to be able to explain what you did, how things work, and why things happen. - Andy Baker [more]
Home
Go Back   Chief Delphi > Technical > Programming > C/C++
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 03-03-2011, 13:40
masoug's Avatar
masoug masoug is offline
Food Consumer
FRC #0114
Team Role: Programmer
 
Join Date: Jan 2010
Rookie Year: 2009
Location: Planet Earth
Posts: 78
masoug is an unknown quantity at this point
Question How Does Compiling Work?

I have been programming the cRIO for two years, and I still haven't been able to grasp how the compiler makes the .out file and how the cRIO links the program on runtime.

I am currently confused about the whole linking process: why do we need WPILib.a when the FRC_UserProgram.out file links on runtime? And what does FRC_UserProgram.out link to when loaded? Why would I get errors about undefined symbols (for WPILib) on runtime?

I greatly appreciate any help!

Thanks!
__________________

JabbaScript
Reply With Quote
  #2   Spotlight this post!  
Unread 03-03-2011, 17:40
PAR_WIG1350's Avatar
PAR_WIG1350 PAR_WIG1350 is offline
Registered User
AKA: Alan Wells
FRC #1350 (Rambots)
Team Role: Alumni
 
Join Date: Dec 2009
Rookie Year: 2009
Location: Rhode Island
Posts: 1,189
PAR_WIG1350 has a reputation beyond reputePAR_WIG1350 has a reputation beyond reputePAR_WIG1350 has a reputation beyond reputePAR_WIG1350 has a reputation beyond reputePAR_WIG1350 has a reputation beyond reputePAR_WIG1350 has a reputation beyond reputePAR_WIG1350 has a reputation beyond reputePAR_WIG1350 has a reputation beyond reputePAR_WIG1350 has a reputation beyond reputePAR_WIG1350 has a reputation beyond reputePAR_WIG1350 has a reputation beyond repute
Re: How Does Compiling Work?

a compiler, in simple terms, simply converts code from a high lever, to a low level (machine code, ones and zeros).
other than that, I don't know much about your particular issue, sorry.
__________________
Reply With Quote
  #3   Spotlight this post!  
Unread 03-03-2011, 17:44
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,098
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: How Does Compiling Work?

Quote:
Originally Posted by PAR_WIG1350 View Post
a compiler, in simple terms, simply converts code from a high lever, to a low level (machine code, ones and zeros).
Ready-to-execute machine code is the result of compiling and linking.

A compiler generally produces object code that needs to be resolved with a linker, not ready-to-execute machine code.

A cross-compiler (like the Windriver installation) produces object code for a target (the cRIO) which differs from the host (Windows PC) on which the compiler is executing.



Reply With Quote
  #4   Spotlight this post!  
Unread 03-03-2011, 19:01
mikets's Avatar
mikets mikets is offline
Software Engineer
FRC #0492 (Titan Robotics)
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2008
Location: Bellevue, WA
Posts: 671
mikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of light
Re: How Does Compiling Work?

Don't know the details of the cRIO platform and the Wind River cross compiler but if it were the Windows platform which I know very well, the compiler compiles the source code into machine code for the target platform. If the code calls some sort of library, then the generated code needs to link to the library code. There are two types of linking: static linking and dynamic linking. Static linking incorporates the library code into the main code as if it is part of your program. So all the calls to the library functions are resolved at compile/link time. Dynamic linking means the actual library code is not incorporated into the main code. Instead, the main code is linked to a stub library which get resolved when your program is loaded into memory at which time, all the stub functions got patched to point to the real library code already in memory. I believe the WPI library is part of the cRIO image. If for some reason the stub library you linked to is a different version from the actual library in the cRIO image, you may have unresolved symbols. But you would not kow until runtime when the dynamic linking takes place.
__________________
Reply With Quote
  #5   Spotlight this post!  
Unread 03-03-2011, 19:16
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,098
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: How Does Compiling Work?

Quote:
Originally Posted by mikets View Post
Don't know the details of the cRIO platform and the Wind River cross compiler but if it were the Windows platform which I know very well, the compiler compiles the source code into machine code for the target platform. If the code calls some sort of library, then the generated code needs to link to the library code.
There's always a linking step. It may be handled seamlessly so you don't see it.

http://www.cprogramming.com/compilingandlinking.html



Reply With Quote
  #6   Spotlight this post!  
Unread 04-03-2011, 00:17
masoug's Avatar
masoug masoug is offline
Food Consumer
FRC #0114
Team Role: Programmer
 
Join Date: Jan 2010
Rookie Year: 2009
Location: Planet Earth
Posts: 78
masoug is an unknown quantity at this point
Re: How Does Compiling Work?

Quote:
Originally Posted by mikets View Post
Don't know the details of the cRIO platform and the Wind River cross compiler but if it were the Windows platform which I know very well, the compiler compiles the source code into machine code for the target platform. If the code calls some sort of library, then the generated code needs to link to the library code. There are two types of linking: static linking and dynamic linking. Static linking incorporates the library code into the main code as if it is part of your program. So all the calls to the library functions are resolved at compile/link time. Dynamic linking means the actual library code is not incorporated into the main code. Instead, the main code is linked to a stub library which get resolved when your program is loaded into memory at which time, all the stub functions got patched to point to the real library code already in memory. I believe the WPI library is part of the cRIO image. If for some reason the stub library you linked to is a different version from the actual library in the cRIO image, you may have unresolved symbols. But you would not kow until runtime when the dynamic linking takes place.
Hmm, stub functions? Like prototypes?
__________________

JabbaScript
Reply With Quote
  #7   Spotlight this post!  
Unread 04-03-2011, 03:55
mikets's Avatar
mikets mikets is offline
Software Engineer
FRC #0492 (Titan Robotics)
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2008
Location: Bellevue, WA
Posts: 671
mikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of light
Re: How Does Compiling Work?

In the Windows platform, when you are calling functions that reside in another DLL, you link to a .lib file. This .lib file contains stub functions that call into an indirect function table. This function table will be resolved (dynamic linked) to the real functions when your program is loaded into memory. This allows multiple programs to link to the same copy of the DLL in memory thus saving memory.
__________________
Reply With Quote
  #8   Spotlight this post!  
Unread 07-03-2011, 23:48
EdenA EdenA is offline
Programmer & Driver
AKA: Anthony Eden
FRC #1540 (The Flaming Chickens)
 
Join Date: Jan 2009
Rookie Year: 2007
Location: Portland, Oregon
Posts: 10
EdenA is an unknown quantity at this point
Re: How Does Compiling Work?

Quote:
Originally Posted by mikets View Post
In the Windows platform, when you are calling functions that reside in another DLL, you link to a .lib file. This .lib file contains stub functions that call into an indirect function table. This function table will be resolved (dynamic linked) to the real functions when your program is loaded into memory. This allows multiple programs to link to the same copy of the DLL in memory thus saving memory.
Actually, I believe that the exe (Portable Executable) file contains a table of import entries at a fixed location in virtual memory in the header of the PE where the program calls to for functions in DLLs to be imported. The Windows program loader at runtime then iterates through the import table entires, patching the code at the entries to JMP to the DLL's imported functions in virtual memory (where they are located at a given time). I believe the .lib file's contents will change whether the library is shared or static.

If the library is static, it's code will be stuck in with the rest of the application's which is using it, and the .lib file will probably contain the object file code (an intermediate form of code before machine code, with stuff such as relocation information) of the library.

If the library is shared, the .lib file will probably contain a list of functions which exist in the shared library. The actual code will be separated from the application's which uses it- in a PE file on Windows (.exe, .dll) or .out in vxWorks. This makes it possible to update the library an application is using without recompiling the application, also promoting modularity and code reuse. The purpose of the .lib file for applications on conventional operating systems here is usually to facilitate the process of "static" linking.

However, a lot of this information is irrelevant to the poster's question.

For programming with the vxWorks operating system on the cRIO, it is important to understand in the compile process the distinction between "static" and "dynamic" linking. On vxWorks, .out files (your program) are linked dynamically. What does this mean?

Lets say you have a C/C++ program with a function declared in a header file, and your code calls it, but you haven't compiled the definition of the function (it is unwritten in your project). On a conventional operating system such as Windows or Unix the compiler will resolve links between functions and function calls at compile time- meaning that the application's exe will know for sure where those functions are once it is constructed.

In other words, if you are using a function which is declared to exist but not defined you will get an error at compile time. This is static linking.

On vxWorks, you will not get an error at compile time. You will get an error at runtime, because when vxWorks loads programs it dynamically links all applications calls to functions at runtime (resolves where functions are at the load time, instead of compile time). This is dynamic linking.

Discovering linker errors at runtime can lead to some hard-to-resolve crashes if you aren't debugging an application when it's run by the vxWorks program loader. So, you should usually always run your program via debug kernel task.

The WPI library is not part of the cRIO image. It is a static library, merged in with the rest of your code at compile time. That's why it is so easy to recompile and use your own version of WPI lib.

For a complete view of the traditional compilation process, I would recommend reading Compilers: Principles, Techniques, and Tools (the dragon book).

Last edited by EdenA : 08-03-2011 at 00:01.
Reply With Quote
  #9   Spotlight this post!  
Unread 08-03-2011, 21:13
masoug's Avatar
masoug masoug is offline
Food Consumer
FRC #0114
Team Role: Programmer
 
Join Date: Jan 2010
Rookie Year: 2009
Location: Planet Earth
Posts: 78
masoug is an unknown quantity at this point
Re: How Does Compiling Work?

Okay, I see how this works now...
Thanks!
__________________

JabbaScript
Reply With Quote
  #10   Spotlight this post!  
Unread 09-03-2011, 01:09
jhersh jhersh is offline
National Instruments
AKA: Joe Hershberger
FRC #2468 (Appreciate)
Team Role: Mentor
 
Join Date: May 2008
Rookie Year: 1997
Location: Austin, TX
Posts: 1,006
jhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond reputejhersh has a reputation beyond repute
Re: How Does Compiling Work?

The thing that makes the VxWorks (and Linux as well) different from what you see on Windows is that even though WPILib is statically linked with your application, the loader has the chance to resolve any missing symbols at runtime. Because of this, even if the linker knows that it can't find all the WPILib symbols when it's linking (because it's the wrong version of the FPGA for instance) it doesn't know that those symbols won't exist when actually loading it, so you get no error.

On Windows, all symbols you want to link against must exist when the application is linked. That's why on Windows you have a stub library to provide those symbols. This allows the link to know if the symbols you expect to have are there and you will get an error when building your program if they aren't. There are no stub libraries on VxWorks. There's always the case where the stub library and the DLL are inconsistent, and you'll get runtime loader errors in that case on Windows.
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 03:06.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi