5 Replies - 366 Views - Last Post: 29 March 2011 - 06:22 PM Rate Topic: -----

#1 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Is there a way to execute code with gcc?

Posted 29 March 2011 - 02:29 PM

For class we use VI to write our code on Linux. We then use the built in gcc compiler to compile our code. I just finished my code and it is finally compiling through gcc without any errors but I there is supposed to be output and I cannot see it.

My question is, is there a way to actually run the code on gcc?

And then, what is your choice of compiler for C?
Is This A Good Question/Topic? 0
  • +

Replies To: Is there a way to execute code with gcc?

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,433
  • Joined: 25-December 09

Re: Is there a way to execute code with gcc?

Posted 29 March 2011 - 02:33 PM

Usually you run the program from the command line where you compiled it. Example: if the program name is myprogram the you would type ./myprogram notice the "./" normally with Linux the current directory is not in the search directory so you need the "./" to indicate that the program is in the current directory.

As for which compiler, for Linux you will usually use gcc for compiling C program. If you want an IDE then I would suggest Code::Blocks.

Jim

This post has been edited by jimblumberg: 29 March 2011 - 02:35 PM

Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5929
  • View blog
  • Posts: 12,851
  • Joined: 16-October 07

Re: Is there a way to execute code with gcc?

Posted 29 March 2011 - 02:55 PM

I use... gcc! ;)

The steps are something like this.

Make a C file, we'll call it foo.c:
#include <stdio.h>

int main() {
	printf("Can you see me now!\n");
	return 0;
}



Compile it, here with some helpful flags:
baavgai@DIC:~/Dev/C$ gcc -Wall -o foo foo.c



Run it:
baavgai@DIC:~/Dev/C$ ./foo 
Can you see me now!
baavgai@DIC:~/Dev/C$



That's about it.
Was This Post Helpful? 2
  • +
  • -

#4 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: Is there a way to execute code with gcc?

Posted 29 March 2011 - 03:27 PM

Hmm, I compiled it fine with just this:

gcc foo.c


Then when I try to run it with this:

./foo.c


it gives me this error:

-bash: ./foo.c: Permission denied


Any idea why?
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,433
  • Joined: 25-December 09

Re: Is there a way to execute code with gcc?

Posted 29 March 2011 - 03:57 PM

With the commands that you used to compile this program you should have a file named a.out in the directory. If there is a file name a.out you will run your program by typing ./a.out

Edit You may want to check out this link on compiling with gcc.

Jim

This post has been edited by jimblumberg: 29 March 2011 - 04:17 PM

Was This Post Helpful? 1
  • +
  • -

#6 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: Is there a way to execute code with gcc?

Posted 29 March 2011 - 06:22 PM

./a.out worked. Thanks for both of yours help. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1