【问题标题】:Code::Blocks output "error: ld returned 1 exit status" without else代码::块输出“错误:ld返回1退出状态”没有其他
【发布时间】:2017-01-04 02:58:32
【问题描述】:

当我构建代码时,我的 Code::Blocks IDE 只调用“错误:ld 返回 1 退出状态”。我经常在“collect2:”中看到这个错误,因为“collect2:ld 返回了 1 个退出状态”。我只知道这是链接器错误。所以我需要包含另一个 linraries。

但是即使是 C++,我也没有使用这个 IDE 的经验。

所以我不明白我是如何摆脱这个错误的。

我想问下面的事情。

1)如何摆脱这个错误。考虑了哪些问题。

2)如何设置构建选项以适合下面的代码。

注意(构建选项): 添加了Linker设置中的链接库

../../opencv-2.4.13/build/lib/libopencv_highgui.so

../../opencv-2.4.13/build/lib/libopencv_core.so

../../opencv-2.4.13/build/lib/libopencv_imgproc.so

pthread

boost_system

而且,搜索目录中的编译器是

../../opencv-2.4.13/include/opencv

../../../../usr/include/boost

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/managed_mapped_file.hpp> 
#include <boost/interprocess/containers/vector.hpp>   // boost/containers/vector.hpp
#include <boost/interprocess/containers/string.hpp>   // boost/containers/string.hpp
#include <iostream>
#include <sys/time.h>
#include <stdio.h>

 //                                                                    void_allocator;
namespace bip = boost::interprocess;

typedef unsigned char uchar;
//Typedefs of allocators and containers
typedef bip::managed_shared_memory::segment_manager                       segment_manager_t;
typedef bip::allocator<void, segment_manager_t>  void_allocator;

typedef void_allocator::rebind<uchar>::other                           uchar_allocator;
typedef bip::vector<uchar, uchar_allocator>                                   uchar_vector;



template <typename Alloc = std::allocator<uchar> >
struct BasicInData {

    public:
        BasicInData(Alloc alloc = {}) : image(alloc)
        { }

        template <typename T>
        BasicInData(double x, int sizeImg, uchar_vector& image, Alloc alloc = {}) :
            x(x), sizeImg(sizeImg), image(alloc)
        { }

        double x = 0;
        int sizeImg = 0;
        uchar_vector image;
};

using InData = BasicInData<>; // just heap allocated

namespace Shared {
    using segment                      = bip::managed_shared_memory;
    using segment_manager              = segment::segment_manager;

    template <typename T> using alloc  = bip::allocator<T, segment_manager>;
    template <typename T> using vector = bip::vector<T, alloc<T> >;

    using InData = BasicInData<alloc<uchar> >; // shared memory version

    vector<InData>& locate(segment& smt) {
        auto* v = smt.find_or_construct<vector<InData> >("InDataVector")(smt.get_segment_manager());
        assert(v);
        return *v;
    }
}


int main(int argc, char* argv[]) {
    if(argc == 1){ //Parent process
        // Remove shared memory on construction and destruction

        // Create a new segment with given name and size
        struct timeval tv;
        gettimeofday(&tv, NULL);
        struct shm_remove
        {
            shm_remove(){bip::shared_memory_object::remove("MySharedMemory");}
            ~shm_remove(){bip::shared_memory_object::remove("MySharedMemory");}
        }remover;
        Shared::segment smt(bip::create_only,"MySharedMemory", 65536); // 10 Kb for coliru
        auto &data = Shared::locate(smt);
        //Shared::alloc bip::alloc_inst (data);

        cv::Mat_<cv::Vec3b> mat;
        cv::VideoCapture vcap(0);

        Shared::InData id(smt.get_segment_manager());


        if (!vcap.isOpened())
            return -1;

        while (1) {
            vcap >> mat;
            int image_size = mat.total() * mat.elemSize();
            id.sizeImg = image_size;
            id.image.resize(image_size * sizeof(uchar));
            memcpy(&id.image[0], mat.data, image_size * sizeof(uchar));
            //Launch child process
            gettimeofday(&tv, NULL);
            double time = ((double)tv.tv_usec/1000000);
            id.x = time;
            data.push_back(id);
            if(cv::waitKey(30) >= 0) break;
        }

        std::string s(argv[0]); s += " child";
        if(0 != std::system(s.c_str()))
            return 1;

        // check child has destroyed the vector
        if(smt.find<Shared::vector<InData>>("InDataVector").first)
            return 1;

    } else{
        // Open the managed segment
        bip::managed_shared_memory segment(bip::open_only, "MySharedMemory");

        // Find the vector using c-string name
        bip::vector<InData> *myvector = segment.find<bip::vector<InData>>("InDataVector").first;
        // Use vector in reverse order

        bip::vector<InData>::iterator it;

        cv::Mat_<cv::Vec3b> im;
        for(it = myvector->begin(); it !=myvector->end(); ++it){
            im.resize(it->sizeImg);
            memcpy(im.data, &it->image[0], it->sizeImg);
            cv::imshow("window1", im);
        }

        segment.destroy<bip::vector<InData>>("InDataVector");

        return 0;
    }
}

【问题讨论】:

  • “构建日志”选项卡显示什么?
  • @greatwolf ,我完全忘记了。我去看看。
  • undefined reference to symbol 'shm_unlink@@GLIBC_2.2.5' 在构建日志中,我需要'-lrt',但我仍然不明白。你能教我在哪里输入“rt”吗?
  • 您可以在项目->构建选项->链接器设置选项卡下添加它。
  • 我会先尝试rt,代码块应该知道如何正确传递标志

标签: c++ opencv boost settings codeblocks


【解决方案1】:

谢谢,@greatwolf。 最后,我可以摆脱这个错误。 现在,我在下面显示“链接器设置”。

../../opencv-2.4.13/build/lib/libopencv_highgui.so

../../opencv-2.4.13/build/lib/libopencv_core.so

../../opencv-2.4.13/build/lib/libopencv_imgproc.so

pthread

boost_system

rt

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-12
    • 2017-08-10
    • 1970-01-01
    相关资源
    最近更新 更多