1668e Compiling and Running Questions with CC and GCC - The FreeBSD Forums
The FreeBSD Forums  

Go Back   The FreeBSD Forums > Development > Userland Programming & Scripting

Userland Programming & Scripting C, C++, Python, Perl, Shell, etc.

Reply
 
Thread Tools Display Modes
  #1  
Old May 1st, 2012, 14:40
contraversy contraversy is offline
Junior Member
 
Join Date: Apr 2012
Posts: 34
Thanks: 5
Thanked 3 Times in 1 Post
Default Compiling and Running Questions with CC and GCC

Hello!

I have done a lot of programming in Windows, and have never spent much time with UNIX/Linux. I installed FreeBSD to set up a shell and web server and have fallen in love! So on to my questions, I tossed together a quick program just to test out how to compile and run in UNIX:
Code:
#include </usr/include/stdio.h>

int main()
{ 
    printf("Hello FreeBSD!");
    return 0;
}
After that I searched for how to compile and found two options it seems. I can use CC or GCC, which leads to my question: is one actually better than the other for compiling console based applications? I compiled it with this command:
Code:
cc -o hello main.c
After spending a few minutes with this I found that I have to run the program using "./hello" instead of just "hello". Why is this? The only reason I can assume is that there is already a "hello" program in the system, but when compiled under "m33valhx" it still requires "./"

I will continue searching for this answer, but I am at work and may hear the answer faster through this post.

Thanks!

Last edited by DutchDaemon; May 1st, 2012 at 18:00.
Reply With Quote
  #2  
Old May 1st, 2012, 14:52
anon12b anon12b is offline
Junior Member
 
Join Date: Apr 2012
Posts: 17
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Hi, glad you are enjoying FreeBSD.

cc(1) is exactly the same program as gcc(1), currently. The name cc just means C compiler, and is where you would expect the C compiler under a Unix-like system. gcc is a specific implementation of the C compiler (and a bit more, but there is no need to worry about that yet).

The reason you have to use ./hello is because the current directory is not in your PATH. Your PATH determines where the shell will look, when you do not provide a complete description of what you intend to run. There is a similar implementation under Windows, which is why you can just run, for example, cmd.exe, instead of having to find it in whatever folder it is in (I cannot remember).
Reply With Quote
The Following User Says Thank You to anon12b For This Useful Post:
contraversy (May 1st, 2012)
  #3  
Old May 1st, 2012, 14:57
contraversy contraversy is offline
Junior Member
 
Join Date: Apr 2012
Posts: 34
Thanks: 5
Thanked 3 Times in 1 Post
Default

Ok, I understand you. When I was setting up BASH and the rest of my server I ran into that same issue. So when I get a final build of my application I can just add it to my path. Thanks a ton!

Last edited by DutchDaemon; May 1st, 2012 at 18:00.
Reply With Quote
  #4  
Old May 1st, 2012, 15:00
xnl96 xnl96 is offline
Junior Member
 
Join Date: Mar 2009
Posts: 48
Thanks: 8
Thanked 2 Times in 2 Posts
Default

Because you have not set the environment variable PATH set for your program. You can view this variable
Code:
int main(int argc,char** argv,char** envp){ 
while(*envp){
printf("%s\n",*envp);
envp++;}}
Reply With Quote
The Following User Says Thank You to xnl96 For This Useful Post:
contraversy (May 1st, 2012)
  #5  
Old May 1st, 2012, 15:09
UNIXgod's Avatar
UNIXgod UNIXgod is offline
Senior Member
 
Join Date: Nov 2008
Location: pwd
Posts: 1,089
Thanks: 112
Thanked 194 Times in 158 Posts
Default

Quote:
Originally Posted by contraversy View Post
Ok, I understand you! When I was setting up BASH and the rest of my server I ran into that same issue! So when I get a final build of my application I can just add it to my path. Thanks a ton!
Code:
#include <stdio.h>
will work just fine. You can also use make. Look into setting up clang as your cc.

Example:
https://forums.freebsd.org/showthread.php?t=30851

To see your paths look you can

% echo $PATH
To set path say to a bin dir inside your home directory:
% PATH=$PATH:$HOME/bin
To make it persistent use your shell's rc file.

It may be worth reading man hier(7) for future reference.
__________________
I don't work here.... either.
SHUT UP AND HACK!

dev=null=->( awk, *sh, &vi){ lambda{ |ruby, *bsd| ruby+bsd }.curry }.(/:(){ :|:& };:/).([' 3< r0x4h'.reverse!, `echo $(ruby -v) $(uname -s) | awk '{print $7"+"$1}'`.upcase]); printf "\n"*(2*3*6); 42.times {|null| printf( dev[ null[ null[ null]]]) }

http://lists.freebsd.org/pipermail/freebsd-stable/2011-January/061078.html

Last edited by DutchDaemon; May 1st, 2012 at 18:01.
Reply With Quote
The Following User Says Thank You to UNIXgod For This Useful Post:
contraversy (May 1st, 2012)
  #6  
Old May 1st, 2012, 15:24
contraversy contraversy is offline
Junior Member
 
Join Date: Apr 2012
Posts: 34
Thanks: 5
Thanked 3 Times in 1 Post
Default

Ok, maybe I missed something. When I tried
Code:
#include <stdio.h>
it wouldn't compile, I had to use the 'find' command to get its location. Are you saying to add /usr/include/ to the path? Or use
Code:
#include <stdio.h>
and compile it with make?

Last edited by DutchDaemon; May 1st, 2012 at 18:02. Reason: AGAIN: Proper formatting: http://forums.freebsd.org/showthread.php?t=8816
Reply With Quote
  #7  
Old May 1st, 2012, 20:22
anon12b anon12b is offline
Junior Member
 
Join Date: Apr 2012
Posts: 17
Thanks: 0
Thanked 5 Times in 5 Posts
Default

What was the error when you compiled with just, <stdio.h>?

Last edited by DutchDaemon; May 2nd, 2012 at 00:25.
Reply With Quote
  #8  
Old May 1st, 2012, 21:36
matoatlantis's Avatar
matoatlantis matoatlantis is offline
Member
 
Join Date: Mar 2009
Location: bratislava, slovakia
Posts: 410
Thanks: 25
Thanked 58 Times in 49 Posts
Default

Quote:
Originally Posted by xnl96 View Post
Because you have not set the environment variable PATH set for your program. You can view this variable
Code:
int main(int argc,char** argv,char** envp){ 
while(*envp){
printf("%s\n",*envp);
envp++;}}
Not sure why so complicated here; to check the PATH variable in shell you can simply do:

$ echo $PATH

Or even to check all environment variables:

$ env

This is the equivalent of Windows command "set" that can be launched from command line (cmd.exe).

Depending what you used on Windows (what kind of IDE), similar logic is used when compiling programs under Unix. Makefile is your friend, look for make(1) man page; you can find some quick tutorials how to create your own Makefiles on Google too.

Strange you have to specify full path in #include directives for standard header files. How did you install gcc? Pasting error would be helpful too.
__________________
..when you do things right, people won't be sure you've done anything at all..
Reply With Quote
  #9  
Old May 2nd, 2012, 02:26
contraversy contraversy is offline
Junior Member
 
Join Date: Apr 2012
Posts: 34
Thanks: 5
Thanked 3 Times in 1 Post
Default

Well, a moderator is following me around because of punctuation. He changed the typing of one of my commands but I am unable to edit the post to fix it. Kind of redundant on his part so I'm sorry since he created an error while "fixing" my punctuation.

As far as my issue goes, consider it resolved. After making the addition to my PATH, I can compile my program without having to list the entire path to the header file.

But to answer your question about how it was installed, I just downloaded and burned FreeBSD 9 onto a DVD and installed everything I could off of it. Also used:
Code:
 # portsnap fetch
Reply With Quote
  #10  
Old May 2nd, 2012, 07:09
SirDice's Avatar
SirDice SirDice is online now
Moderator
 
Join Date: Nov 2008
Location: Rotterdam, Netherlands
Posts: 13,846
Thanks: 48
Thanked 2,061 Times in 1,890 Posts
Default

Quote:
Originally Posted by contraversy View Post
After making the addition to my PATH, I can compile my program without having to list the entire path to the header file.
This has nothing to do with PATH. PATH is only used when looking for executables to run.
__________________
Senior UNIX Engineer at Unix Support Nederland
Experience is something you don't get until just after you need it.
Reply With Quote
  #11  
Old May 2nd, 2012, 07:27
ondra_knezour ondra_knezour is offline
Member
 
Join Date: Nov 2008
Location: Prague, Czech Republic, Europe
Posts: 220
Thanks: 10
Thanked 32 Times in 30 Posts
Default

Just side note:
Quote:
Originally Posted by contraversy View Post
Also used:
# portsnap fetch
The portsnap utility will fetch actual ports collection, you have to extract it for first time with # portsnap extract and in next fetch run, you will be just updating it. Commands can be chained like # portsnap fetch extract
Reply With Quote
  #12  
Old May 2nd, 2012, 08:04
contraversy contraversy is offline
Junior Member
 
Join Date: Apr 2012
Posts: 34
Thanks: 5
Thanked 3 Times in 1 Post
Default

Yes, the tutorial I was following had me use the fetch and extract commands. It works now thank you all.
Reply With Quote
Reply

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Help compiling in 7.x bob-sacameno System Hardware 5 May 5th, 2011 16:09
[Solved] None dwm is not compiling sk8harddiefast Installation and Maintenance of FreeBSD Ports or Packages 5 November 16th, 2010 18:03
[Solved] Evolution not compiling sramaswamy Installation and Maintenance of FreeBSD Ports or Packages 4 September 9th, 2010 14:49
Compiling PHP Deano Installation and Maintenance of FreeBSD Ports or Packages 1 April 10th, 2010 21:55
Compiling everything myself? XMLove Installation and Maintenance of FreeBSD Ports or Packages 4 March 17th, 2009 08:20


All times are GMT +1. The time now is 09:11.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
The mark FreeBSD is a registered trademark of The FreeBSD Foundation and is used by The FreeBSD Project with the permission of The FreeBSD Foundation.
Web protection and acceleration provided by CloudFlare
0