Jump to content

GCC


Recommended Posts

Hi I start taking a C class on-line, I will do it on gcc, and I kinda fell behind the "hello world" because gcc played me a not that cute trick.

 

$gcc -c hellow_world.c

$ls hello_world*

hello_world.c

hello_world.o

$chmod +x hello_world.o

$./hello_world.c

BASH: Unable to execute bynary

 

looking around the net I found this:

On a redhat system the script to enable/disable

binfmt_misc is /etc/rc.d/init.d/ia64fmt.

You should try executing this script manually as root and see

what happens.

 

I went to that address and found nothing, well I did, but didnt found that script. Any way I can get BASH to recognize and execute .o?

 

[moved from Software by spinynorman]

Link to comment
Share on other sites

i think that was not a trick. you just compiled your binary into an intermediary file and not an executable file. can you try doing it this way:

 

 $ gcc -o hello hello.c 
$ chmod +x hello
$ ./hello

 

and post the results? im not able to check it myself so there might be some corrections on the instructions.

 

ciao!

Link to comment
Share on other sites

i think that was not a trick. you just compiled your binary into an intermediary  file and not an executable file. can you try doing it this way:

 

Ok It worked now I wanted to know, why, what happened, why it didnt let me as a binary. Any problems being a binary? what exactly is an intermediary file?

Link to comment
Share on other sites

usually compiled languages are translated from source code (the one that humans can read) to intermediary code (or object files).

 

the intermediary code is then linked together to create an executable file. when you passed the -c option to the gcc compiler, you only instructed it to compile the source code to its intermediary representation.

 

the -o option means that you are instructing the gcc compiler to compile the source code and link it to the 'hello' executable.

 

binary files mean that a file is not human-readable. executable files does not necessarily mean an executable (like in the case of the intermediare object file hello.o). executables can be human-readable files (like bash and perl scripts) as well.

 

 

did i manage to answer your question to some degree or did i just managed to confuse you even more? :)

 

ciao!

Link to comment
Share on other sites

So I will need to learn something about linking to fully understand the difference. I understand that binary in this case is more like encript than actually building an executable.

 

The question about this would be more like:

ok but I made a binary AND I make it executable (through chmod +x) so why it didnt execute. Did bash didnt interpretate the .bin files (since I have executed bin files before).

Link to comment
Share on other sites

an overview on how compiled languages would be very nice.

 

and encrypted file also does not imply a binary file. a file can be encrypted while still being pure text. encryption basically means that you put something through a cipher which makes the content hard to understand.

 

when you created an intermediary file, you created a binary file. when you performed 'chmod +x' on it, you gave it execute permissions. performing that action does not mean that a file is already an executable. it just means that it has the needed rights to be executed by the user. the difference is that you would need the file to be in a proper format before it can be run/executed.

 

a not so exact analogy on the source->intermediary->executable binary process would be like baking a cake. you can gather the ingredients (source code) like flour, eggs, etc. and then using a mixer or spatula (compiler) you can mix it all to make a dough (intermediary files). as it is, the dough is not equal to a cake. you cant discern the individual ingredients anymore (akin to being a binary, you cant read it anymore). the next step would be to put the dough in the oven (linker) and wait for all the ingredients to mingle and become the cake (final executable).

 

 

extensions (like .bin) has no meaning in *nix systems. it is just for the benefit of the users. even if you rename x.bin to x, and if it retains the same file permissions then you would still be able to execute it.

 

maybe this link can be of better help.

 

ciao!

Link to comment
Share on other sites

Objects are generally used to compile small parts of a larger code. Imagine you wanted to include a new skin for your media player you created. Instead of adding the skin into the main trunk of code you'd usually just place it into a separate file, then compile both the main trunk and your skin into objects, the linker then moulds the two together into a single executable.

 

Most modern compilers can do the object compiling/linking in one step, by using the -c flag in your original post you instructed gcc to compile only, and not to link. As previously mentioned, the -o flag (along with your desired exe name) will compile and link with gcc.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...