BASH脚本编译多个c++文件- OpenCV

BASH script to compile multiple C++ files - OpenCV

本文关键字:文件 OpenCV c++ 脚本 编译 BASH      更新时间:2023-10-16

初始问题请参见c++和OpenCV中其他文件中的调用函数。我使用的代码在那里给出了详细。这是一个子问题。

我有一个BASH脚本:
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%.*}"

以上BASH脚本用于使用./script.sh input.cpp编译OpenCV代码。
我的疑问是我如何修改它来同时编译多个文件,就像我在主代码中使用的一些函数的另一个文件:
./script.sh input.cpp functions.cpp


编辑
按照John B的建议,我将BASH脚本修改为

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

但现在,我得到以下错误:

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

正如Jonathan Leffler建议的那样,使用makefile是更好的实践。但是如果您想使用Bash脚本,您可以使用$@调用所有参数并遍历每个参数。

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

对于编辑中提到的问题:

当文件没有"main"函数时,你需要使用-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

一个简单明了的解决方案:

创建另一个脚本文件,内容如下:

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

然后,不带参数地运行它:

./doit.sh

您的新脚本doit.sh必须与您现有的script.sh位于同一文件夹中。

请考虑花时间学习一个像样的构建系统,比如cmake。对于一个小项目来说,这可能是多余的,但当你的项目规模扩大时,它就变得值得投资了。

与其调试,不如直接编写。试试吧:

#!/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