Linux CentOS 7和Ubuntu 20.04.1 LTS之间的共享库差异 [英] Shared library disparity between Linux CentOS 7 and Ubuntu 20.04.1 LTS

查看:239
本文介绍了Linux CentOS 7和Ubuntu 20.04.1 LTS之间的共享库差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将用C编写的项目从CentOS 7(核心)移植到Ubuntu 20.04.1 LTS(Focal Fossa)系统.该项目严重依赖< cpuset.h> 库,并在CentOS系统上正确编译和执行.但是,当我尝试在Ubuntu系统上使用cpuset.h中的函数时,出现未定义引用"错误.

I am porting a project written in C from a CentOS 7 (Core) to an Ubuntu 20.04.1 LTS (Focal Fossa) system. The project relies heavily on the <cpuset.h> library, and compiles and executes correctly on the CentOS system. However, when I try to use functions from cpuset.h on the Ubuntu system, I get 'undefined reference' errors.

以下存储在文件 test.c 中的代码可以在CentOS上编译并正确运行:

The following code, stored in file test.c, compiles and runs correctly on CentOS:

#define _GNU_SOURCE

#include<stdio.h>
#include <cpuset.h>

int main(){

    int x = cpuset_version();
    printf("cpuset lib version: %d\n",x );

    return 0;
}

我如何编译: gcc -Wall -O2 -std = gnu99 -g -lcpuset test.c -o test

输出:

[xxxx@CentOS]$ ./test 
cpuset lib version: 3

但是,当我尝试在Ubuntu系统上编译相同的 test.c 文件时,出现此错误:

However, when I try to compile the same test.c file on the Ubuntu system, I get this error:

xxxx@Ubuntu:$ gcc -Wall -O2 -std=gnu99 -g -lcpuset test.c -o test
/usr/bin/ld: /tmp/ccpxlk4F.o: in function `main':
test.c:8: undefined reference to `cpuset_version'
collect2: error: ld returned 1 exit status

此外,这不限于< cpuset.h> 库.我尝试使用< pthread.h> 中的一个简单函数,它也给了我同样的错误.任何人都可以帮助确定为什么我不能在Ubuntu系统上使用共享库吗?预先感谢

Furthermore, this is not limited to the <cpuset.h> library. I tried to use a simple function from <pthread.h> and it also gave me the same error. Can anyone help with identifying why I cannot use shared libraries on the Ubuntu system? Thanks in advance

推荐答案

由于OP的问题是GCC参数的顺序错误(许多指南的顺序确实不正确!),正如对该问题的评论中所讨论的那样,我相信保证有一个最小的 Makefile 可以处理这些问题:

Since OP's issue is wrong order of parameters to GCC (many guides do show an incorrect order!), as discussed in the comments to the question, I believe showing a minimal Makefile to handle these is warranted:

CC      := gcc
CFLAGS  := -Wall -O2 -g
LDFLAGS := -lcpuset
TARGETS := test

.PHONY: all clean

all: $(TARGETS)

clean:
    rm -f *.o $(TARGETS)

%.o: %.c
    $(CC) $(CFLAGS) -c $^

test: test.o
    $(CC) $(CFLAGS) $^ $(LDFLAGS) -o $@

请注意,Makefile中的缩进必须使用 Tab 而不是空格.由于此论坛将 Tab s转换为空格,因此您将需要修复上述makefile,例如,通过运行 sed -e's | ^ * | \ t |'-i Makefile .

Note that the indentation in Makefiles must use Tabs and not spaces. Since this forum converts Tabs to spaces, you will need to fix the above makefile, for example by running sed -e 's|^ *|\t|' -i Makefile.

如果您想直接将 foo.c 编译为可执行文件,则配方为

If you want to compile say foo.c directly to an executable, the recipe is

foo: foo.c
    $(CC) $(CFLAGS) $^ $(LDFLAGS) -o $@

您只需要运行 make (默认情况下是在当前目录中使用Makefile,默认目标是第一个目标,位于名为 all 的目标文件之上),以重新编译TARGET(这里是 test ,但是您可以通过在行中添加以空格分隔的内容来提供更多内容).

You only need to run make (it defaults to using the Makefile in the current directory, and the default target is the first one, above the one named all), to recompile the TARGETS (here, test, but you can supply more by just adding them space-separated to the line).

您还可以运行 make clean test 从"scratch"重建 test ,即先删除所有临时文件和所有目标.

You can also run make clean test to rebuild test from "scratch", i.e. removing all temporary files and all targets first.

您可以通过简单地在命令行中提供变量来覆盖诸如 CFLAGS 之类的变量.例如, make CFLAGS ="-Wall -Wextra -Os";清理所有以使用不同的编译标志重新编译所有内容.

You can override variables like CFLAGS by simply supplying them on the command line; for example, make CFLAGS="-Wall -Wextra -Os" clean all to recompile everything with different compilation flags.

这篇关于Linux CentOS 7和Ubuntu 20.04.1 LTS之间的共享库差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆