bash脚本编译多个C ++文件 - OpenCV的 [英] BASH script to compile multiple C++ files - OpenCV

查看:242
本文介绍了bash脚本编译多个C ++文件 - OpenCV的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅<一个href=\"http://stackoverflow.com/questions/24442836/call-functions-in-other-files-in-c-and-opencv/\">Call在C ++和OpenCV 初始问题的其他文件的函数。我使用的code详细给出了那里。这是一个子问题。

我有一个bash脚本:

 回声编译$ 1
如果[[$ 1 ==的* .c]
然后
    GCC -ggdb`pkg配置--cflags opencv` -o`基本名$ 1 .c` $ 1` pkg配置--libs opencv`;
ELIF [[$ 1 == *的.cpp]
然后
    G ++ -ggdb'pkg配置--cflags opencv` -o`基本名$ 1 .cpp` $ 1` pkg配置--libs opencv`;
其他
    回声请只编译.c和.cpp文件
科幻
回声输出文件= GT; $ {1%*。}

以上bash脚本是用来使用编译OpenCV的code ./ script.sh input.cpp 。结果
我的疑问是我如何可以修改它在同一时间编译多个文件,像我用我的主要code像一些功能的另一个文件中:结果
./ script.sh input.cpp functions.cpp


修改结果
正如约翰·乙建议,我修改我的bash脚本,以

 在$ @F;做
    回声编译$ F
    如果[[$ F ==的* .c]
    然后
        GCC -ggdb`pkg配置--cflags opencv` -o`基名$ F.c`$ F`pkg配置--libs opencv`
    ELIF [$ F == *的.cpp]
    然后
        G ++ -ggdb'pkg配置--cflags opencv` -o`基名$ F.cpp`$ F`pkg配置--libs opencv`
    其他
        回声$ f不是.c和.cpp文件
    科幻
    回声输出文件= GT; $ {。F%*}
DONE

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

 编译draw_shapes.cpp
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o:在功能`_start:
(+的.text 0x20的):未定义引用'主'
collect2:错误:LD返回1退出状态
输出文件= GT; draw_shapes
编译TEST.CPP
/tmp/ccW65wuP.o:在函数'主':
/家庭/迦腻色迦/桌面/ OpenCV的测试/ TEST.CPP:31:未定义的引用`draw_line(CV ::垫,CV :: Point_&LT; INT&gt;中CV :: Point_&LT; INT&gt;中INT,INT,INT)
collect2:错误:LD返回1退出状态
输出文件= GT;测试


解决方案

要问题,在编辑说明:

要获得编译时,他们没有主功能的文件,你需要使用 -c 开关,这样编译器生成的可执行文件,而不是对象。该PROCES应该是这样的:

  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

Please see Call functions in other files in C++ and OpenCV for initial problem. The code I am using is given there in detail. This is a subproblem.

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 using ./script.sh input.cpp.
My doubt is how can I modify it to compile multiple files at the same time, like another file with some functions I am using in my main code like:
./script.sh input.cpp functions.cpp


EDIT
As suggested by John B, I modified 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

解决方案

To problem stated in edit:

To get files compiled when they don't have the "main" function, You will need to use -c switch, so compiler produces objects instead of executables. The proces should look like this:

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

When You have the objects done, it is time for linking them together

g++ obj/* -o my_awesome_executable

这篇关于bash脚本编译多个C ++文件 - OpenCV的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆