PDA

View Full Version : Compiling C From Terminal?


Trip
06-09-2002, 03:50 PM
I'm just starting out in C programming today! I've done REALbasic before, but decided it's time to move onto something that can actually get me where I want to go.

So, I know it's possible to compile c from terminal using the 'cc' command, but what I don't know is:

How to load up my .c file into terminal for compiling.

I know how to locate the directory the file is in, but i have no clue how to load it into terminal, any help?

Thanks!!!

mervTormel
06-09-2002, 04:25 PM
does this example help any?

$ pwd
/Users/merv/work

$ cat dirread.c

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>

int main(int argc, const char * argv[])
{
int i;
char *c;
DIR *dirp;
struct dirent *dp;

dirp = opendir(argv[1]);
if (dirp)
{
while ((dp = readdir(dirp)) != NULL)
{
printf("good readdir\n");
printf("d_fileno %d\n", (int) dp->d_fileno);
printf("d_reclen %d\n", (int)dp->d_reclen);
printf("d_type %d\n", dp->d_type);
printf("d_namlen %d\n", dp->d_namlen);

c = dp->d_name;
for ( i = 0; i < dp->d_namlen; i++ ,c++ )
{
printf( "char %d - as a num - %d as a char \
%c\n", i, (int)*c, *c );
}
printf( "%s\n" , dp->d_name);
printf("======================\n");
}
(void)closedir(dirp);
}
}

$ cc -o dirread dirread.c # compile dirread.c to outfile dirread

$ ll dirread*
-rwxr-xr-x 1 merv test 9.4k Jun 9 13:17 dirread*
-rw-r--r-- 1 merv test 909 Jun 1 07:51 dirread.c

$ ./dirread .
good readdir
d_fileno 214238
d_reclen 12
d_type 4
d_namlen 1
char 0 - as a num - 46 as a char .
...

Trip
06-09-2002, 11:04 PM
erm, not really. I already figured out how to compile my app now though on my own, hehehe. Anyway...

...when I compile my code I get a file called "a.out" in the same directory as the .c file...what do I do with it? Is it suppost to be there after I compile? Help! :)

mervTormel
06-09-2002, 11:28 PM
hypothetically...

% cc --help
Usage: cc [options] file...
Options:
--help Display this information
(Use '-v --help' to display command line options of sub-processes)
...
-o <file> Place the output into <file>
...

% cc foo.c # creates the binary executable a.out by default

% ll a.out
-rwxr-xr-x 1 merv test 9.4k Jun 9 20:07 a.out

% cc -o foo foo.c # will name the output binary "foo" instead of a.out

% ll foo
-rwxr-xr-x 1 merv test 9.4k Jun 9 20:08 foo

foo is the executable, the program that you want to run...

% ./foo # run foo
hello, world.

you've got some studying to do...

% man cc

CC(1) System General Commands Manual CC(1)

NAME
cc - C and Objective-C compiler

DESCRIPTION
This command is documented in /Developer/Documentation/Developer-
Tools/Compiler. Please refer to the documentation files in that directory
for more information.

mervTormel
06-10-2002, 12:21 AM
regarding your novice status, i was remembering when this kind of stuff looked like sanskrit to me, and i wanted to qualify why my post is so terse.

if the above examples are bewildering, then you've got a gap in your foundation that will need to be fleshed out.

when the above becomes trivial, you will be well on your way.

keep diggin'

osxpez
06-23-2002, 04:36 AM
If you don't give cc a name for the compiled executable cc gives it the name a.out. Use the -o <filename> argument on cc to name your executable something else. Often it's best to do a simple Makefile so you can use "make" to compile your project. Read "man gcc" and "man make".

cookieme
07-06-2002, 03:46 AM
Ok I managed to compile my code in the terminal using the information on this page, but when I type:

./"codename" (without the quotes)

I get a BUS ERROR and the program crashes! What should I do?

stetner
07-06-2002, 03:56 AM
Fix your code! :)
From
http://www.nacs.uci.edu/help/manuals/uci.unix.guide/common_mistakes_and_pitfalls/segmentation.html
Bus error (core dumped)

The program you're running has a bug. A copy of
the program's memory has been placed in the file
'core' in your current directory (unless something
else went wrong also). If it's your own program,
you're probably using a null pointer or
referencing past one end of an array or string. Be
sure to delete the 'core' file.

If it's a system program, please report it to a
consultant, being sure to specify how the problem
occurred, including the commands you typed and the
files you were using. Also tell the consultant the
location of the 'core' file; don't delete it.

cookieme
07-06-2002, 04:39 AM
Did you get my private message!

stetner
07-06-2002, 06:59 AM
Yes, just tried to reply and it says your mailbox is full. Your problem is that your 'str' variable is a pointer to a string, no storage is allocated so when you try to put things into it you end up writing to memory you do not have allocated.

Try

char str[10];

As well, you should ensure the string is NULL terminated IE:
for(k=0;k<8;k++) str[k] = (rand()%93)+33;
str[8] = '\0';
printf("%s ",str);