【问题标题】:Undefined reference resulting from g++ linkingg++ 链接产生的未定义引用
【发布时间】:2010-02-21 05:15:05
【问题描述】:

我是 g++ 和 Makefile 的新手。我正在尝试链接这个 BeBOP SMC 库,它位于我的 lib 目录中。 lib 目录下是 bebop_util 和 sparse_matrix_converter,这两个都是已经构建好的,没有错误。我在 bebop_util 下看到 libbebop_util.a、libbebop_util.so,在 sparse_matrix_converter 下看到 libsparse_matrix_converter.a、libsparse_matrix_converter.so。以下是出处:

生成文件

CC=g++
CFLAGS=-c -Wall

test.out: test.o
    $(CC) -o test.out -Ilib/sparse_matrix_converter/include -Llib/bebop_util \
-Llib/sparse_matrix_converter -lbebop_util -lsparse_matrix_converter test.o

test.o: test.cpp
    $(CC) $(CFLAGS) -Ilib/sparse_matrix_converter/include test.cpp

clean:
    rm -f test.o test.out

test.cpp

#include <bebop/smc/sparse_matrix.h>
#include <bebop/smc/sparse_matrix_ops.h>

int main(int argc, const char* argv[])
{
    struct sparse_matrix_t* A = load_sparse_matrix (MATRIX_MARKET, "sample_input");
    destroy_sparse_matrix(A);
    return 0;
}


输出:
login3% make
g++ -c -Wall -Ilib/sparse_matrix_converter/include test.cpp
g++ -o test.out -Ilib/sparse_matrix_converter/include -Llib/bebop_util -Llib/sparse_matrix_converter -lbebop_util -lsparse_matrix_converter test.o
test.o: In function `main':
test.cpp:(.text+0x1a): undefined reference to `load_sparse_matrix(sparse_matrix_file_format_t, char const*)'
test.cpp:(.text+0x27): undefined reference to `destroy_sparse_matrix(sparse_matrix_t*)'
collect2: ld returned 1 exit status
make: *** [test.out] Error 1

请注意,test.cpp 依赖于 sparse_matrix_converter,而后者又依赖于 bebop_util。你能告诉我我可能犯了什么错误吗?谢谢。

汤姆

【问题讨论】:

  • 您正在链接 -lbebop_util -lsparse_matrix_converter,这两个名称看起来都不像是主库。你确定你链接了所有东西吗?
  • 感谢您的评论。我想我已经与一切联系在一起了。 sparse_matrix_converter 是主库。该库称为 BeBOP SMC。 SMC代表稀疏矩阵转换器。

标签: linker g++


【解决方案1】:

BeBOP 代码看起来是 C 代码,但没有添加正确的 C++ 保护。用extern "C" 包围你的包含来解决这个问题:

extern "C" {
#include <bebop/smc/sparse_matrix.h>
#include <bebop/smc/sparse_matrix_ops.h>
}

【讨论】:

  • 谢谢。添加 extern C 后,编译错误消失了。但是,我收到了一个新错误:./test.out:加载共享库时出错:libbebop_util.so:无法打开共享对象文件:没有这样的文件或目录
  • 我通过 chmod 777 确保 libbebop_util.so 的权限不是问题。
  • 我会接受答案,因为新问题似乎是另一个问题。我会发布新问题。
猜你喜欢
  • 2013-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-24
  • 1970-01-01
  • 2011-08-28
  • 1970-01-01
相关资源
最近更新 更多