# Compiling and Running Questions with CC and GCC



## contraversy (May 1, 2012)

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:

```
#include </usr/include/stdio.h>

int main()
{ 
    printf("Hello FreeBSD!");
    return 0;
}
```

After that I searched for how to compile and found t*w*o 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:

```
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!


----------



## anon12b (May 1, 2012)

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).


----------



## contraversy (May 1, 2012)

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!


----------



## xnl96 (May 1, 2012)

Because you have not set the environment variable *PATH* set for your program. You can view this variable 
	
	



```
int main(int argc,char** argv,char** envp){ 
while(*envp){
printf("%s\n",*envp);
envp++;}}
```


----------



## UNIXgod (May 1, 2012)

contraversy said:
			
		

> 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!




```
#include <stdio.h>
```

will work just fine. You can also use *make*. Look into setting up clang as your cc.

*E*xample:
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.


----------



## contraversy (May 1, 2012)

Ok, maybe I missed something. When I tried 
	
	



```
#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? *O*r use 
	
	



```
#include <stdio.h>
```
 and compile it with make?


----------



## anon12b (May 1, 2012)

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


----------



## _martin (May 1, 2012)

xnl96 said:
			
		

> Because you have not set the environment variable *PATH* set for your program. You can view this variable
> 
> 
> 
> ...



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.


----------



## contraversy (May 2, 2012)

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:

```
# portsnap fetch
```


----------



## SirDice (May 2, 2012)

contraversy said:
			
		

> 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.


----------



## ondra_knezour (May 2, 2012)

Just side note:


			
				contraversy said:
			
		

> 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`


----------



## contraversy (May 2, 2012)

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


----------

