how to run a C program with a test file? - c

I compile the program with gcc
gcc -Wall -ansi -pedantic -o program.c
what is the command to run it with my test file?
./program <test1.txt> ???

Your gcc is wrong and at present errors on building.
$ gcc -Wall -ansi -pedantic -o program.c
Will show this error:
gcc: fatal error: no input files
compilation terminated.
Try:
gcc -Wall -ansi -pedantic -o program program.c
This will compile your program into an exec called program, which you can then run by;
./program
Then maybe try running you're command passing in the test1.txt
./program test1.txt
Please see comments in your OP as people have already offered good advise.

Related

How to run a C program which uses string.h in Ubuntu

My C program uses string.h.. Initially I was not able to compile it. But then I used
$ gcc filename.c -E
Then it complied but I am not able to run it with both
./a.out
./filename
The -E option to gcc invokes only the preprocessor. If you want to compile you need to do this:
gcc -g -Wall -Wextra -o filename filename.c
The -o option specifies the name of the executable to create, the -W options enable the common compiler warnings, and -g includes debugging symbols so you can use tools such as gdb to step through the code line by line.

Building and debugging a program with makefile

As part of my university studies I learn C programming. I'm using Eclipse Juno IDE.
Here is the problem: I have few .c and .h files, and a makefile made by our course staff. I want to build the project with this makefile. I spent hours searching all over the internet how to build and debug it, but everytime I think I got it an error jumps.
Can someone please explain once and for always how to do this process - build the project with the given makefile, and run it in debug mode so I can debug it?
It is important to mention that sometimes I failed to build the project and got the following message:
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/5.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot open output file Assignment2altVersion.exe: Permission denied
However, I managed to build the project several times, but when I tried to run it in debug mode it was stuck either on "Launching: Configuring GDB" or "Launching C/C++ Application".
EDIT:
there are 6 files: SP_Stack.c, SP_Stack.h, SP_Aux.c, SP_Aux., SP_Stack_UnitTest.c, main.c
SP_Aux.c and main.c #include SP_Aux.h.
SP_Stack.c, SP_Aux. and SP_Stack_UnitTest.c #include SP_Stack.h
here's the makefile:
ex2: main.o SP_Aux.o SP_Stack.o
gcc -std=c99 -Wall -Werror -pedantic-errors main.o SP_Aux.o SP_Stack.o -o ex2
Stack_UnitTest: SP_Stack.o SP_Stack_UnitTest.o
gcc -std=c99 -Wall -Werror -pedantic-errors SP_Stack.o SP_Stack_UnitTest.o -o Stack_UnitTest
main.o: main.c SP_Aux.h SP_Stack.h
gcc -std=c99 -Wall -Werror -pedantic-errors -c main.c
SP_Stack.o: SP_Stack.c SP_Stack.h
gcc -std=c99 -Wall -Werror -pedantic-errors -c SP_Stack.c
SP_Stack_UnitTest.o: SP_Stack.h SP_Stack_UnitTest.c
gcc -std=c99 -Wall -Werror -pedantic-errors -c SP_Stack_UnitTest.c
SP_Aux.o: SP_Aux.c SP_Aux.h
gcc -std=c99 -Wall -Werror -pedantic-errors -c SP_Aux.c
clean:
rm -f main.o SP_Aux.o SP_Stack.o SP_Stack_UnitTest.o Stack_UnitTest ex2
the 6 files I mentioned and the makefile are all in the same directory.
I just tried it all over again: went to the right side of the screen to the "make targets" tab. I chose my project and created a target named "makefile" with "make" as build command. then I double clicked it and the project finishe building. when I tried to run it in debug mode it gets stuck on 96% with a message: "Launching: configuring GDB"

Compiling C code in Linux terminal

I am using Linux mint 16. I had a code that I change it a bit.
I use two following commands in terminal in order to run the code. The problem is that it does not give me any error but the changes are not applied, which means it runs the previous version of code.
gcc -std=c99 -c Code.c -o Code.o
./Code
gcc -std=c99 -c Code.c -o Code.o will put the compiled object file in Code.o, not ./Code as you expect it to be..
Also, -c tells do not run the linker. So effectively you end up with an object file which cannot be run.
gcc -std=c99 Code.c -o Code will produce what you need.
For a complete list of gcc flags either use man gcc or see http://linux.die.net/man/1/gcc
Try
gcc -std=c99 -c Code.c -o Code
./Code

gcc makefile won't compile

My final executable (this is in unix though) will be proj07.
proj07: /user/cse320/Projects/project07.driver.o proj07.support.o
gcc -Wall /user/cse320/Projects/project07.driver.o proj07.support.o
proj07.support.o: proj07.support.c
gcc -c proj07.support.c
This creates proj07.support.o but no proj07 exists after compilation. I don't get an error so my mistake must be simple but I can't seem to figure it out.
Here's the output:
gcc -c proj07.support.c
gcc -Wall /user/cse320/Projects/project07.driver.o proj07.support.o
Also I am to use a static driver to test my file which is why the path is like that
You probably do have an a.out executable. Add -o $# to your first gcc occurrence and you should be fine.

When running a compiled GCC file in the terminal, nothing happens

I have Ubuntu 64 bit installed, and when I compile C files using the flags:
gcc -g -m32 -ansi -Wall -c -o *.o *.c
it compiles the files, but when I try to run them in the terminal, nothing happens.
So I decided to try to compile and run just one simple file without a makefile, with the following code:
#include <stdio.h>
int main()
{
printf("Hello World");
return 0;
}
The compilation succeeds but when I try to run the file I get nothing...
Note: I already tried to install lib32gcc1, libc6-i386, and g++-multilib.
How can I fix this problem?
Replace -o *.o by -o programname. The -o parameter receives the executable name of the program you're generating. And here is gcc manual:
http://gcc.gnu.org/onlinedocs/gcc-4.7.2/gcc/Overall-Options.html#Overall-Options
For a simple test, leave every option out:
> cat test.c
#include <stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}
> gcc test.c
> ./a.out
Hello World
and see if that works.
Let us assume you (already) have two object files a.o and b.o, their corresponding source file a.c and b.c and some common header ch.h; then your (incorrect) command line
gcc -g -m32 -ansi -Wall -c -o *.o *
might be expanded as:
gcc -g -m32 -ansi -Wall -c -o a.o b.o a.c b.c ch.h a.o b.o
(actually, that would be even worse, e.g. if you have some Makefile or some backup files from your editors like a.c~, as remarked by William Pursell)
which would compile but not link, the files b.o a.c b.c ch.h a.o b.o which does not means much.
You should understand that the shell is expanding first the arguments before executing any gcc program (in a new process). To understand what is expanded, consider replacing gcc by echo (or by gcc -v which would show what is really happening)
Then, you should read the GCC documentation about invoking GCC
Actually, you need to spend several hours in reading, e.g. Advanced Bash Scripting Guide (which does have some mistakes perhaps) and Advanced Linux Programming. Several wikipedia pages could also be useful to read.

Resources