BASH script to compile multiple files in C ++ - OpenCV

Refer to the Call Functions in other C ++ and OpenCV files for the original problem. The code I use is given in detail there. This is a subtask.

I have a BASH script:

echo "compiling $1"
if [[ $1 == *.c ]]
then
    gcc -ggdb `pkg-config --cflags opencv` -o `basename $1 .c` $1 `pkg-config --libs opencv`;
elif [[ $1 == *.cpp ]]
then
    g++ -ggdb `pkg-config --cflags opencv` -o `basename $1 .cpp` $1 `pkg-config --libs opencv`;
else
    echo "Please compile only .c or .cpp files"
fi
echo "Output file => ${1%.*}"

The above BASH script is used to compile OpenCV code with ./script.sh input.cpp.
My doubt is how I can change it to compile several files at the same time, like another file with some functions that I use in my main code, for example:
./script.sh input.cpp functions.cpp


EDIT
As John B suggested, I changed my BASH script to

for f in "$@"; do
    echo "compiling $f"
    if [[ $f == *.c ]]
    then
        gcc -ggdb `pkg-config --cflags opencv` -o `basename "$f" .c` "$f" `pkg-config --libs opencv`
    elif [[ $f == *.cpp ]]
    then
        g++ -ggdb `pkg-config --cflags opencv` -o `basename "$f" .cpp` "$f" `pkg-config --libs opencv`
    else
        echo "$f is not .c or .cpp file"
    fi
    echo "Output file => ${f%.*}"
done

But now I get the following error:

compiling draw_shapes.cpp
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
Output file => draw_shapes
compiling test.cpp
/tmp/ccW65wuP.o: In function `main':
/home/kanishka/Desktop/opencv test/test.cpp:31: undefined reference to `draw_line(cv::Mat, cv::Point_<int>, cv::Point_<int>, int, int, int)'
collect2: error: ld returned 1 exit status
Output file => test
+4
4

, make . Bash script, $@ .

for f in "$@"; do
    echo "compiling $f"
    if [[ $f == *.c ]]
    then
        gcc -ggdb `pkg-config --cflags opencv` -o `basename "$f" .c` "$f" `pkg-config --libs opencv`
    elif [[ $f == *.cpp ]]
    then
        g++ -ggdb `pkg-config --cflags opencv` -o `basename "$f" .cpp` "$f" `pkg-config --libs opencv`
    else
        echo "$f is not .c or .cpp file"
    fi
    echo "Output file => ${f%.*}"
done
+1

, :

, , "" , -c, . :

g++ $src_file1 $src_file2 -Iincludes_directory -c -o obj/object1.o
g++ $src_file3 $src_file4 -Iincludes_directory -c -o obj/object2.o
g++ $src_file5 $src_file6 -Iincludes_directory -c -o obj/object3.o
....
g++ $srcfileX $src_file_containing_main_function -Iincludes_directory -c -o obj/object_with_main.o

,

g++ obj/* -o my_awesome_executable
+1

:

script :

./script.sh input.cpp
./script.sh functions.cpp

:

./doit.sh

script, doit.sh , script.sh.

Please consider investing time in learning a decent build system like cmake. This may be unnecessary for a small project, but it is worth the investment when your projects grow in size.

0
source

Instead of debugging, it’s easier to just write. Give this:

#!/bin/bash

test -n "$1" || { echo "error: insufficient input, usage :${0##*/} filename.c (cpp)"; exit 1; }

for source in "$@"; do

    ext="${source##*\.}"  # file extension
    fname="${source##*/}" 
    fname="${fname%\.*}"  # base filename (no extension)

    if test "$ext" == "c" ; then

        gcc -ggdb `pkg-config --cflags opencv` \
        -o "$fname" "$source" \
        `pkg-config --libs opencv`

    elif test "$ext" == "cpp" ; then

        g++ -ggdb `pkg-config --cflags opencv` \
        -o "$fname" "$source" \
        `pkg-config --libs opencv`

    else
        echo "Please compile only .c or .cpp files"

    fi

done

exit 0
0
source

Source: https://habr.com/ru/post/1546226/


All Articles