When compiling with gcc I used time to get compile time.
time gcc main.c && ./a.out
when trying to do the similar thing in clang I can't get the result
clang -time main.c && ./a.out
gives me a waring:
clang: warning: argument unused during compilation: '-time'
probably my approach is false, so please help me find compile time using clang
Try clang main.c -ftime-report
Neither gcc time … nor clang -time … make any sense. You're looking for the time shell command (or shell keyword), which you can put before any command.
time gcc main.c && ./a.out
time clang main.c && ./a.out
This is assuming bash or a similar shell such as zsh or ksh (or even fish).
Related
I wrote a code in C in Ubuntu which checks for balanced brackets in the input given.I compiled it using gcc compiler and I am getting the correct output.This is actually a part of an online course and they are asking me to use the compiler flag
gcc -pipe -O2 -std=c11 filename -lm
I don't think I understand what I am supposed to do so I tried compiling using this flag and my code is not compiling. My question is if my code compiles when I do
gcc filename.c
why isn't it compiling when I do
gcc -pipe -O2 -std=c11 filename -lm
The error message I am getting is :
cc1plus: warning: command line option ‘-std=c11’ is valid for C/ObjC but not for C++
The reason is the file ending. A capital C is interpreted as a C++ file. The solution is to just rename the file like this:
mv filename.C filename.c
My question is if my code compiles when I do gcc filename.C why isn't it compiling when I do gcc -pipe -O2 -std=c11 <filename> -lm
See above. But there are some situations where it would not solve everything. While C11 gives some extensions to previous versions, it's not 100% backwards compatible.
-std=c11 is a correct option
however you need at least gcc 4.7 or higher to have this option
By "<filename>" they mean to substitute in the name of the file you want to compile. Including the literal string "<filename>" will not work.
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.
Everytime I compile and run c file, I have to type:
gcc filename.c
a.out
I don't want to do this in two lines, How to compile and run in one line on linux terminal?
Try
gcc filename.c && a.out
It only runs the second command if the first was successful. See https://askubuntu.com/questions/334994/which-one-is-better-using-or-to-execute-multiple-commands-in-one-line
You can separate commands with a ;. So for example:
gcc filename.c; ./a.out
However, you probably only want to run a.out if the compile was successful. For this you can use &&:
gcc filename.c && ./a.out
The Quick and Dirty Solution is Wrong
The quick and dirty solution is a very bad idea in C:
gcc myfile.c && ./a.out
This will not run the executable if the compilation fails, but when compilation succeeds the code will automatically run even if warnings are issued; in C you should always at least inspect warnings before attempting to run code. For the most part you should just never run code that compiles with warnings. Often running code with warnings will mean that code has some undefined behavior; you should not be running such blindly. Of course, with warnings at a minimum as in the above code, there may not be a lot of warnings, when there should be, anyway. At the absolute minimum, one should use:
gcc myfile.c -Wall -Wextra -Werror && ./a.out
Using -Wall -Wextra will issue warnings for a lot of silly mistakes that bring the undefined behavior, and -Werror keeps code compiled with warnings from automatically running.
A Better Solution
To solve this problem, and type less, I used to use this bash script saved as crepl in my search path:
#!/bin/bash
gcc -std=c99 -Wall -Wextra -Wpedantic -Werror $1 -o tempout &&\
./tempout && rm tempout
When I wanted to quickly test some source code saved in, e.g., myfile.c, I could enter at the command-line:
crepl myfile.c
The code will not run if compilation fails, and it will not run if it compiles with warnings thanks to -Werror. If compilation is successful, the program runs, and the temporary executable is removed after running.
Improvements
Since originally writing this answer I have evolved my solution into a slightly fancier bash script that accepts optional further arguments to the compiler, linking libraries, etc.
#!/usr/bin/env bash
# crun
#
# A script to invoke gcc, build the executable, execute the binary,
# and cleanup after. The script will exit without running the executable
# if there are any compiler errors or warnings.
#
# This script uses -std=c18, but it could probably be modified so that
# the version is taken from a command-line parameter, defaulting to c18.
#
# Any commands following the crun invocation are appended to CMD.
CMD="gcc -std=c18 -Wall -Wextra -Wpedantic -Werror"
TEMPFILE="tempfile$$"
for ARG in "$#"
do
CMD+=" $ARG"
done
CMD+=" -o ${TEMPFILE} && ./${TEMPFILE} && rm ${TEMPFILE}"
eval $CMD
Now if I need to link in, e.g., the math library:
crun myfile.c -lm
does the trick, failing if there are any errors or warnings (which are turned up to reasonable levels), and cleaning up after itself.
Explanation:
A ; B – Run A and then B, regardless of the success or failure of A
A && B – Run B only if A succeeded
A || B – Run B only if A failed
You wanted how to compile and run.
I give you how to write, compile and run.
cat << EOF > temp.c && gcc temp.c ; ./a.out ; rm temp.c a.out
#include <stdio.h>
int main(){ printf("%s\n", "Hello World!"); return 0; }
EOF
If you use such a thing fairly regularly you might also be interested in this:
#!/bin/sh
# GCC in the streams
temp="temp.c"
sgcc() {
(
rm "$temp" # just to be sure
# append every line from shell to the temp file
while read -r l; do printf "%s\n" "$l" >>"$temp"; done
cat "$temp"
gcc "$temp"
exitcode=$?
./a.out
rm "$temp" a.out
exit $exitcode
)
}
Which you can call like below:
sgcc << EOF
#include <stdio.h>
int main(){ printf("%s\n", "Hello World!"); return 0; }
EOF
I have to turn off optimizations while compiling c code I wrote while using the gcc compiler on a linux. I have found that I can compile the code but I can only get the code to compile without the executable name specified (default to a.out).
So this works:
gcc -O0 Problem04b.c
But my problem is that I have to submit this assignment and I can't submit an executable called a.out because my instructor needs to know which problem it is. I realize I can probably just run
cp a.out Problem04b
then
rm a.out
but I want to know if there is a way I can just compile the code directly into the executable Problem04b. I've tried to run the command like this:
gcc -O0 Problem04b Problem04b.c
but I'm having no luck.
Thanks for your help.
It's the -o flag:
gcc -O0 -o Problem04b Problem04b.c
To specify the output file, you need to use the -o <filename> option with gcc.
Note : Please mind the lower case here
In your case, it should be
gcc -O0 -o Problem04b Problem04b.c
For reference: From gcc manual
-o file
Place output in file file. This applies to whatever sort of output is being produced, whether it be an executable file, an object file, an assembler file or preprocessed C code.
Actually, you also want to get warnings (that won't change the produced executable, but it is very helpful to you), so compile with
gcc -O0 -Wall -Wextra Problem04b.c -o Problem04b
The -Wall option asks for nearly all warnings, the -Wextra option asks for even more of them.
To run your thing (the ./ is useful because of possible PATH issues):
./Problem04b
Notice that -O0 is optional (since it is the default), you could remove it.
gcc -Wall -Wextra Problem04b.c -o Problem04b
If you want real optimization, e.g. for benchmarking, use e.g. -O1 or -O2 or -O3
You probably want to compile with debug information, then
gcc -g -Wall -Wextra Problem04b.c -o Problem04b
and of course you need to learn how to use the GDB debugger. So read some tutorial about that, then type
gdb ./Problem04b
You'll get a (gdb) prompt. Try help at that time.
You probably want to read the chapter about invoking GCC of the GCC documentation.
I want to know if this is possible:
if you put gcc filename.c you will compile filename.c in a.out file
and if you put ./a.out you will execute the file.
The composite version it's
gcc filename.c && ./a.out
I want edit this command in bash_profile to do this in one short command line.
gcc filename.c do this gcc filename.c && ./a.out
You can declare a function:
gccrun () {
gcc "$1" && ./a.out
}
Be careful, though: what if someone uses full path to the C file as a parameter?
Makefiles are usually used for this kind of stuff.
a.out: file.c
gcc $<
run: a.out
./a.out
make run would run a.out recompiling it if its source has changed since the last compilation or doesn't exist.