BASH脚本编译多个c++文件- OpenCV
BASH script to compile multiple C++ files - OpenCV
初始问题请参见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
相关文章:
- 在C++代码中包含opencv时,使用ctypes创建.so文件
- OpenCV 4.2.0 文件存储分段错误
- 加载"D:UsersPublicDocumentsopencvbuildx64vc14binopencv_world340d.dll",找不到或打开PDB文件
- OpenCV 3 Visual Studio 2017 调试,调用堆栈没有.pdb文件
- OpenCV 安装错误:没有这样的文件或目录"pyopencv_generated_include.h"
- 如何在Visual Studio中添加OpenCV文件
- 无法在 CLion 中编译 OpenCV 头文件
- 使用 Opencv 将姿势值实时写入文本文件
- 如果函数使用 OPENCV Mat 作为输入,如何编写头文件?
- OPENCV - 如何在OpenCV中使用Haar Cascade Github xml文件?
- CUDA opencv 构建失败 - 缺少实际存在的文件
- 在头文件中使用opencv类型来实现未定义的标识符
- 帧提取 使用OpenCV的视频文件开头只有几秒钟
- 从YML文件OPENCV C 初始化摄像机参数
- 从文件OpenCV读取视频
- 如何从XML文件(OpenCV)将一定数量的行读入Mat对象
- 将矢量<Mat>转换为矢量文件 Opencv
- 在Linux上构建opencv_controib示例可执行文件(opencv 3.0.0-alpha)
- BASH脚本编译多个c++文件- OpenCV
- 当对象靠近时将帧保存到文件 opencv.