[英]Installing and using a shared library
I have a project I'm working on that currently has a matrix.c
file containing a some functions and has a corresponding header matrix.h
.我有一个正在处理的项目,目前有一个matrix.c
文件,其中包含一些函数,并有一个相应的 header matrix.h
。 I made a shared library libccalc.so
using matrix.c
and another file.我使用matrix.c
和另一个文件创建了一个共享库libccalc.so
。 I am trying to create a directory in /usr/local/lib
and a directory in /usr/local/include
, both called ccalc
to house the respective .so
and .h
files which can then later be used to compile programs using the functionality of libccalc.so
.我正在尝试在/usr/local/lib
中创建一个目录,在/usr/local/include
中创建一个目录,这两个目录都称为ccalc
以容纳各自的.so
和.h
文件,这些文件稍后可用于使用该功能编译程序libccalc.so
的。 However, when I do this I am getting an error.但是,当我这样做时,出现错误。
To be precise, matrix.c
contains functions:准确地说, matrix.c
包含函数:
Matrix *mat_create(int rows, int cols)
, Matrix *mat_create(int rows, int cols)
,
void mat_fill(Matrix *mat, double *entries)
and void mat_fill(Matrix *mat, double *entries)
和void mat_print(Matrix *mat)
which are declared in matrix.h
.在matrix.h
中声明。 I place the files in their respective directories as explained above, I run ldconfig /usr/local/lib/ccalc
and I make a new file test.c
in some other directory with the following:如上所述,我将文件放在各自的目录中,运行ldconfig /usr/local/lib/ccalc
并在其他目录中创建一个新文件test.c
,内容如下:
// test.c
#include <stdio.h>
#include "matrix.h"
int main() {
Matrix *m = mat_create(2, 2);
double entries[4] = {1, 2, 3 ,4};
mat_fill(m, entries);
mat_print(m);
return 0;
}
matrix.h
contains the following: matrix.h
包含以下内容:
// matrix.h
#ifndef MATRIX_H
#define MATRIX_H
#define MAX_SIZE 50
typedef struct Matrix Matrix;
struct Matrix {
int rows;
int cols;
double *data;
};
Matrix *mat_create(int n, int p);
void mat_fill(Matrix *mat, double *entries);
void mat_print(Matrix *mat);
#endif
When I enter the command: gcc -I/usr/local/include/ccalc -L/usr/local/lib/ccalc -lccalc test.c -o test
, I get the error:当我输入命令时: gcc -I/usr/local/include/ccalc -L/usr/local/lib/ccalc -lccalc test.c -o test
,我收到错误:
/usr/bin/ld: /tmp/ccesD44J.o: in function `main':
test.c:(.text+0x26): undefined reference to `mat_create'
/usr/bin/ld: test.c:(.text+0x71): undefined reference to `mat_fill'
/usr/bin/ld: test.c:(.text+0x7d): undefined reference to `mat_print'
collect2: error: ld returned 1 exit status
However, when I place libccalc.so
and matrix.h
in /usr/local/lib
and /usr/local/include
, enter ldconfig
and enter the command gcc -L/usr/local/lib/ccalc -lccalc test.c -o test
, and run ./test
, it works perfectly但是,当我将libccalc.so
和matrix.h
放在/usr/local/lib
和/usr/local/include
时,输入ldconfig
并输入命令gcc -L/usr/local/lib/ccalc -lccalc test.c -o test
,然后运行./test
,它完美运行
Can someone tell me what I am doing wrong?有人能告诉我我做错了什么吗?
The order of program arguments to gcc
matters a lot.程序 arguments 到gcc
的顺序很重要。 Please read the documentation about Invoking GCC .请阅读有关调用 GCC的文档。
Also, test
is often a builtin command.此外, test
通常是一个内置命令。 See test(1) .请参阅测试 (1) 。
So (on Linux) rename test.c
as prog.c
then compile your program with所以(在 Linux 上)将test.c
重命名为prog.c
然后编译你的程序
gcc -Wall -Wextra -g prog.c -L/usr/local/lib/ccalc -lccalc -o prog
Then use strace(1) and gdb(1) to understand the behavior of prog
, and ldd(1) , objdump(1) with readelf(1) to analyze prog
.然后使用strace(1)和gdb(1)了解prog
的行为,并使用ldd(1) 、 objdump(1)和readelf(1)分析prog
。 See also elf(5) and ld.so(8) .另见elf(5)和ld.so(8) 。
Regarding building shared libraries, read Drepper's paper How to write shared libraries关于构建共享库,请阅读 Drepper 的论文How to write shared libraries
You probably want to compile matrix.c
into libcalc.so
using你可能想编译matrix.c
到libcalc.so
使用
gcc -fPIC -Wall -O -shared -g matrix.c -o libcalc.so
but you might need other program arguments.但您可能需要其他程序 arguments。
Regarding matrix operations, this answer could be inspirational.关于矩阵运算,这个答案可能是鼓舞人心的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.