【问题标题】:Compiling in gcc dynamically with objects in other folders使用其他文件夹中的对象在 gcc 中动态编译
【发布时间】:2018-02-08 16:07:21
【问题描述】:

我正在开发一个使用有序文件夹的项目。我想要一个名为 /bin 的文件夹来存储所有二进制文件和一个名为 /src 的文件夹,其中包含所有 .c 和 .h 文件。

程序将求解方程组,所有与矩阵计算相关的函数将被动态编译。

我有这个文件夹:

/project/src/matrixlib(它将是包含所有矩阵计算的动态库)

有文件:

矩阵.c

#include<stdio.h>
#include"matrix.h"


int matrix_alloc(int n, int m, Matrix *matrix);
{
//do whatever, this is not the problem
}

矩阵.h

#ifndef matrix_h__
#define matrix_h__

struct Matrix
{
//definition here
};typedef struct Matrix Matrix;

extern int matrix_alloc(int, int, Matrix*);
#endif //matrix_h__

我编译这个文件夹:

gcc -c -Wall -Werror -fpic matrix.c

gcc -shared -o libmatrix.so matrix.o

然后我有文件夹:

/project/src/main

有文件:

main.c(使用函数“matrix_alloc”并包括“matrix.h”)

#include<stdio.h>
#include "matrix.h"

int main(void)
{
    Matrix matrix;
    matrix_alloc(3,3,&matrix);
    return 0;
}

我这样编译:

gcc -I/project/src/matrixlib -L/project/src/matrixlib -Wl,-rpath=/project/src/matrixlib -Wall main.c -o  main

但是我遇到了这个错误:

/tmp/cc8kLMIe.o: In function `main':
main.c:(.text+0x34): undefined reference to `matrix_alloc'
collect2: error: ld returned 1 exit status

但我不太明白发生了什么,因为 matrix_alloc 在 matrix.h 文件夹中。

你能帮帮我吗?

谢谢

【问题讨论】:

  • gcc ... main.c ... 之后你不会错过一些-lmatrix吗?

标签: c gcc include dynamic-programming


【解决方案1】:

您没有将libmatrix.so 与您的程序相关联。将-lmatrix 添加到您的编译器调用中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-09
    • 2010-10-24
    • 1970-01-01
    • 2021-06-03
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    相关资源
    最近更新 更多