gpt4 book ai didi

c++ - 用于编译多个 C++ 文件的 BASH 脚本 - OpenCV

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:10:03 38 4
gpt4 key购买 nike

请参阅Call functions in other files in C++ and 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

关于c++ - 用于编译多个 C++ 文件的 BASH 脚本 - OpenCV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24443065/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com