【问题标题】:undefined reference BUT header is there未定义的参考但标头在那里
【发布时间】:2014-12-02 01:13:38
【问题描述】:

我正在尝试运行 ezsift 库示例。该示例的名称为“image_match.cpp”。

这里是代码

image_match.cpp

#include <iostream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#include "../ezsift.h"

using namespace std;

#define USE_FIX_FILENAME 0
int main(int argc, char ** argv)
{
#if USE_FIX_FILENAME
    char * file1 = "img1.pgm";
    char * file2 = "img2.pgm";
#else
    if (argc != 3)
    {
        printf("Please input two image filenames.\n");
        printf("usage: image_match img1 img2\n");
        return -1;
    }
    char file1[255];
    char file2[255];
    memcpy(file1, argv[1], sizeof(char) * strlen(argv[1]));
    file1[strlen(argv[1])] = 0;
    memcpy(file2, argv[2], sizeof(char) * strlen(argv[2]));
    file2[strlen(argv[2])] = 0;
#endif

    // Read two input images
    ImageObj<uchar> image1, image2;
    if(image1.read_pgm(file1) != 0)
    {
        printf("Failed to open input image1!\n");
        return -1;
    }

    if(image2.read_pgm(file2) != 0)
    {
        printf("Failed to open input image2!\n");
        return -1;
    }
    printf("Image 1 loaded. Image size: %d x %d\n", image1.w, image1.h);
    printf("Image 2 loaded. Image size: %d x %d\n", image2.w, image2.h);

    // Double the original image as the first octive.
    double_original_image(true);

    // Detect keypoints
    list<SiftKeypoint> kpt_list1, kpt_list2;
    printf("\nSIFT detection on image 1 ...\n");
    sift_cpu(image1, kpt_list1, true);
    printf("# keypoints in image1: %d\n", kpt_list1.size());

    printf("\nSIFT detection on image 2 ...\n");
    sift_cpu(image2, kpt_list2, true);
    printf("# keypoints in image2: %d\n", kpt_list2.size());

    // Save keypoint list, and draw keypoints on images.
    char filename[255];
    sprintf(filename, "s_A_keypoints.ppm");
    draw_keypoints_to_ppm_file(filename, image1, kpt_list1);
    export_kpt_list_to_file("s_A_keypoints.key", kpt_list1, true);

    sprintf(filename, "s_B_keypoints.ppm");
    draw_keypoints_to_ppm_file(filename, image2, kpt_list2);
    export_kpt_list_to_file("s_B_keypoints.key", kpt_list2, true);

    // Match keypoints.
    list<MatchPair> match_list;
    match_keypoints(kpt_list1, kpt_list2, match_list);

    // Draw result image.
    sprintf(filename, "s_A_B_matching.ppm");
    draw_match_lines_to_ppm_file(filename, image1, image2, match_list);
    printf("# of matched keypoints: %d\n", match_list.size());

    return 0;
}

ezsift.h

#ifndef EZSIFT_H
#define EZSIFT_H

#include "util/image.h"
#include "util/img_io.h"
#include <vector>
#include <list>
.
.
.
#endif

#define DEGREE_OF_DESCRIPTORS (128)
// Enable doubling of original image.
void double_original_image(bool doubleFirstOctave);

// Detect keypoints and extract descriptor.
int sift_cpu(
    const ImageObj<uchar> &image, 
    std::list<SiftKeypoint> & kpt_list, 
    bool bExtractDescriptors);

// Match keypoints from two keypoint lists.
int match_keypoints(
    std::list<SiftKeypoint> & kpt_list1,
    std::list<SiftKeypoint> & kpt_list2,
    std::list<MatchPair> & match_list);

.
.
.
#endif

(我不会在这里包含所有代码,我只是想给出一个想法。下载源代码并查看我正在谈论的文件很容易)强>

还有一个“ezsift.cpp”文件,“util”文件夹中有更多文件

我正在尝试从终端运行它。我进入目录并输入“gcc image_match.cpp”,但据说没有定义 double_original_image、sift_cpu(以及头文件中的所有其他函数)。

更具体地说是给出此错误:

image_match.cpp:(.text+0x1c7): undefined reference to `double_original_image(bool)'
image_match.cpp:(.text+0x20d): undefined reference to `sift_cpu(ImageObj<unsigned char> const&, std::list<_SiftKeypoint, std::allocator<_SiftKeypoint> >&, bool)'

(当然,这些不是唯一的错误。它对所有功能都给出相同的错误)

然后我尝试了“gcc ../ezsift.cpp image_match.cpp”但同样的错误。

我做错了什么??我正在运行 ubuntu 14.04。

【问题讨论】:

  • 你为什么使用memcpy+strlen?为什么不只是strcpy
  • 您需要对库进行链接!否则,编译器肯定知道所有函数,因为它们是在头文件中声明的,但它们的实际定义(链接器必须让你的函数调用指向)是未知的。
  • 函数只是在头文件中声明,没有定义。您需要链接到定义的实际库。
  • @Cameron 你能告诉我怎么做吗?运行“gcc”命令时还需要添加什么?
  • @Dimitris:看过有问题的库后,它似乎没有附带一个预编译的二进制文件来链接(这在 linux 世界中很常见)。您必须:1) 自己构建一个库,然后链接它,或者 2) 与您自己的 (g++ ../ezsift.cpp image_match.cpp) 一起编译 ezsift .cpp 文件。我看到您已经尝试了第二个选项,它应该可以工作 - 但您正在编译代码,就好像它是 C,而不是 C++:使用 g++ 而不是 gcc

标签: c++ gcc undefined undefined-reference


【解决方案1】:

(将 Cameron 的评论扩展为经过测试的答案)

您需要使用您的程序 (image_match.cpp) 编译 ezsift.cppimg_io.cpp,同时提供头文件的位置 (-I\path\to\directory)。这是一个有效的 sn-p,假设您在 examples 目录中:

g++ image_match.cpp  ../ezsift.cpp ../util/img_io.cpp -I.. -o image_match

【讨论】:

    猜你喜欢
    • 2021-06-02
    • 2016-06-15
    • 2014-06-19
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多