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.
Related
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).
I am using FORTRAN to solve partial differentiate equations. Main program and subroutines have been put in .f file. And I got a .sh file to compile the commands in source code in linux operating system. This file has been attached. But I failed to run this. After struggling for a week, I really need some help on this. Please any help would be greatly appreciated!!!
#!/bin/bash
#
mkdir temp
cd temp
rm *
~/binc/$ARCH/f77split ../fishpack.f
#
for FILE in `ls -1 *.f`;
do
gfortran -c -g $FILE >& compiler.txt
if [ $? -ne 0 ]; then
echo "Errors compiling " $FILE
exit
fi
rm compiler.txt
done
rm *.f
#
ar qc libfishpack.a *.o
rm *.o
#
mv libfishpack.a ~/libf77/$ARCH
cd ..
rmdir temp
#
echo "Library installed as ~/libf77/$ARCH/libfishpack.a."
This looks like the shell script is simply trying to compile a fortran file, but instead of using gfortran's internal toolchain, it is compiling parts manually then linking them together. I have a feeling (though I haven't confirmed) that the call to the program ar is bad, even if it got that far (I'm guessing it should be ar -qc instead of ar qc.).
Anyway, if all the source is in a single fortran file that someone else gave you (fishpack.f), you might be able to compile the whole thing with a single call to gfortran:
gfortran fishpack.f
It should create (by default) an output executable with a filename a.out. If the fortran code is not structured such that it can be lumped into a single file, you may need to work on separating some things out (--as well as updating to at least f90, though that's an aside--).
Good luck.
I am trying to do this:
I want to call a make (Makefile exists in some other directory, abc path can be used) from a shell script located in a different directory. How do I do this?
Since shell scripting does not allow me to cd into the Makefile directory and execute make, how can I write the shell command (by giving path to the Makefile to be executed) to execute make?
GNU make accepts many options, notably -C to change directory before running, and -f for giving the Makefile to follow.
Combine them appropriately.
Consider using remake to ease debugging (notably with -x) of Makefile related issues. With GNU make version 4 or better, also use make --trace...
You could have your own executable shell script (e.g. in your $HOME/bin/ which would be in your $PATH) which uses both cd and make).
You could consider other build automation tools (ninja perhaps)
Read also P.Miller's paper Recursive Make considered harmful
I have few header files in /my/path/to/file folder. I know how to include these files in new C program but everytime I need to type full path to header file before including it. Can I set some path variable in linux such that it automatically looks for header files ?
You could create a makefile. A minimal example would be:
INC_PATH=/my/path/to/file
CFLAGS=-I$(INC_PATH)
all:
gcc $(CFLAGS) -o prog src1.c src2.c
From here you could improve this makefile in many ways. The most important, probably, would be to state compilation dependencies (so only modified files are recompiled).
As a reference, here you have a link to the GNU make documentation.
If you do not want to use makefiles, you can always set an environment variable to make it easier to type the compilation command:
export MY_INC_PATH=/my/path/to/file
Then you could compile your program like:
gcc -I${MY_INC_PATH} -o prog src1.c src2.c ...
You may want to define MY_INC_PATH variable in the file .bashrc, or probably better, create a file in a handy place containing the variable definition. Then, you could use source to set that variable in the current shell:
source env.sh
I think, however, that using a makefile is a much preferable approach.
there is a similar question and likely better solved (if you are interested in a permanent solution): https://stackoverflow.com/a/558819/1408096
Try setting C_INCLUDE_PATH (for C header files) or CPLUS_INCLUDE_PATH (for C++ header files).
Kudos:jcrossley3
I'm not in Linux right now and I can't be bothered to reboot to check if everything's right, but have you tried making symbolic links? For example, if you are on Ubuntu:
$ cd /usr/include
$ sudo ln -s /my/path/to/file mystuff
So then when you want to include stuf, you can use:
#include <mystuff/SpamFlavours.h>
I have a project where there are several helper scripts that call the main executable with different command-line options. Right now, the scripts assume the executable is in the same directory, so the calls to the executable in the script look like ./my_program. This, however, is not very flexible. What if the program is installed in the /usr/bin directory, and is not in the current directory?
Is there a way, using automake or autoconf, to generate these scripts, and substitute the calls to the executable with either ./my_program or just my_program, depending on whether or not the executable is already installed?
Sure. IMO the simplest solution with autotools would be:
create new m4 macro under m4/ folder that finds a path of your program, and sets it to a variable.
For example, you created a macro:
MY_PROGRAM_PATH_CHECK([action-if-found], [action-if-not-found])
This macro creates MY_PROGRAM_PATH variable if path is found.
configure.ac
MY_PROGRAM_PATH_CHECK(,[AC_MSG_ERROR([my_program path not found, woot?])
AC_SUBST(MY_PROGRAM_PATH)
AC_CONFIG_FILES([src/script1.sh], [chmod +x src/script1.sh])
AC_CONFIG_FILES([src/script2.sh], [chmod +x src/script2.sh])
convert your scripts to .in files, so the substitution would happen:
src/Makefile.am
bin_SCRIPTS = script1.sh script2.sh
src/script1.sh
#MY_PROGRAM_PATH#/my_program --option1
src/script2.sh
#MY_PROGRAM_PATH#/my_program --option2