how to run gcc command in script to exe main.c - linux

I'm new in linux trying to run .C program from another directory in script. script is in home directory and .C program is in Desktop directory. here is script.
#!/bin/bash
chmod 777 myscript
cd /home/unifi-007/Desktop/
gcc main -o main.c
./main
But i'm not getting it right. how to execute main.c in script.

The usage of gcc is clearly wrong, it should be gcc -o main main.c.
BTW, .C is a suffix for C++ (at least for GCC it is), not C.
BTW again, normally, you do not run a C source file, you compile it, and run the executable file generated by compiler (by linker, actually).

Related

How to compile and run a c program in Linux terminal?

compile using gcc that the compiled code be saved with a different name and run it. I need the commands
To compile file.c and create executable file do
gcc -Wall -Werror -o file file.c
If you need additional libraries, add them after the C file.
To run the program do
./file

How to print the compilation directory recored in linux elf binary?

The gdb docs say the compilation directory may be recorded in the executable file https://sourceware.org/gdb/current/onlinedocs/gdb/Source-Path.html
Executable programs sometimes do not record the directories of the
source files from which they were compiled, just the names. Even when
they do, the directories could be moved between the compilation and
your debugging session.
Assuming the executable file records the compilation directory. How to print the compilation directory ?
Like so:
cd /tmp
gcc -g t.c
readelf -Wwi a.out | grep DW_AT_comp_dir
<15> DW_AT_comp_dir : (indirect string, offset: 0x2ab): /tmp

bash script uses gcc in a loop to compile all

I need write bash script that uses gcc in a loop to compile all .c source files in current directory into .o object files, and additionally creates .s assembly listings in Intel format. Then, the .o files are linked into run executable.
for i in *
do
gcc -type -f "*.c"
done
i just began that but I'm not sure I began it correctly
Rather them writing a bash script, it's acutally better to write a makefile which is specialised in automatizing this kind of things.

How to make scons make gcc with assembly file during compilation?

For example, I've got a 'b.c' file, I compile and generate the code, while I wish to see an intermediate assembly file, I can do under shell like this:
gcc -S b.c
gcc -c b.c
gcc b.o -o b
My question is, how to specify inside scons SConstruct, with all these steps, to make sure I get b.s, b.o and 'b' executable? Seems scons only support functions like Program, Object, Library, so I only get b.o and 'b'. But how to make out 'b.s', while I don't wish to introduce any duplicated work by the compiler(to save time).
Thanks.

How can I make GCC output not executable?

I'm on Ubuntu and a certain .so file generated is executable. When I hit tab to autocomplete I get "something.so main.executable" instead of just "main.executable" which is a bit of an annoyance.
Is there a flag to pass to the linker to avoid the chmod or do I have to manually chmod -x after the fact?

Resources