gpt4 book ai didi

c - 不同的编译方式导致同一个动态库产生不同的结果

转载 作者:太空宇宙 更新时间:2023-11-04 04:11:09 25 4
gpt4 key购买 nike

我使用不同的编译方式来编译我的项目,以在加载动态库时测试同一封面的符号,但是,在不同的编译中有两种不同的结果,我找不到这两种编译之间的差异,请帮忙?

当我使用dlopen()dlsym()时,我看到官方对dlsym()的描述如下:

The dlsym() function shall search for the named symbol in all objects loaded automatically as a result of loading the object referenced by handle (see dlopen()). Load ordering is used in dlsym() operations upon the global symbol object. The symbol resolution algorithm used shall be dependency order as described in dlopen().

注意:加载顺序是一种在加载同名符号时处理冲突的机制。所以我想为 load ordering 运行测试,但是,当使用不同的编译时我发现不同的结果(因为我认为其中两个是等价的)

这是我的项目:

文件树:

├── CMakeLists.txt

├── fn.c

└── main.c

CMakeLists.txt

project(demo)
set(CMAKE_CXX_FLAGS " -fpic ${CMAKE_CXX_FLAGS}")
add_library(fn SHARED fn.c)

add_executable(demo main.c)
target_link_libraries(demo -ldl)

fn.c

#include<stdio.h>


int temp = 1;
void fn_print(){
printf("fn temp address is %p, value is %d\n", &temp, temp);

}

主.c

#include<stdio.h>
#include<dlfcn.h>

typedef void (*fn_p)();
int temp = 0;

int main(){
printf("main temp address is %p, value is %d\n", &temp, temp);

void *handle = dlopen("./libfn.so", RTLD_NOW);
fn_p dy_print = dlsym(handle, "fn_print");
dy_print();

return 0;
}

首先,我使用cmake编译这个项目并测试,我们可以看到变量temp在obj文件和动态库中共享相同的地址0x60104c和值0。

root@acnszavl00033:~/temp/dylib_test# cmake .
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /root/temp/dylib_test
root@acnszavl00033:~/temp/dylib_test# make
Scanning dependencies of target demo
[ 25%] Building C object CMakeFiles/demo.dir/main.c.o
[ 50%] Linking C executable demo
[ 50%] Built target demo
Scanning dependencies of target fn
[ 75%] Building C object CMakeFiles/fn.dir/fn.c.o
[100%] Linking C shared library libfn.so
[100%] Built target fn
root@acnszavl00033:~/temp/dylib_test# ./demo
main temp address is 0x60104c, value is 0
fn temp address is 0x60104c, value is 0

根据描述,varaible temp应该有不同的地址,然后我用gcc只重新编译demo(不要改动动态库libfn.so),然后测试,varaible temp 有不同的地址,为什么?

root@acnszavl00033:~/temp/dylib_test# gcc -o demo main.c -ldl
root@acnszavl00033:~/temp/dylib_test# ./demo
main temp address is 0x60104c, value is 0
fn temp address is 0x7f795cec0028, value is 1

我希望使用 cmake 的 compilatoin 输出应该是这样的:

main temp address is 0x60104c, value is 0
fn temp address is 0x7f795cec0028, value is 1

最佳答案

下面的 CMakeLists.txt 似乎解决了这个问题:

cmake_minimum_required(VERSION 3.4)
project(demo)

add_library(fn SHARED fn.c)

add_executable(demo main.c)
target_link_libraries(demo PRIVATE dl)

如果您不添加 cmake_minimum_required 或只提供低于 VERSION 3.4 的内容,就会遇到问题。否则,当您构建并运行时,您会得到以下信息:

~/so/build> cmake ..
-- The C compiler identification is GNU 9.1.0
-- The CXX compiler identification is GNU 9.1.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/aytekin/so/build
~/so/build> cmake --build .
Scanning dependencies of target fn
[ 25%] Building C object CMakeFiles/fn.dir/fn.c.o
[ 50%] Linking C shared library libfn.so
[ 50%] Built target fn
Scanning dependencies of target demo
[ 75%] Building C object CMakeFiles/demo.dir/main.c.o
[100%] Linking C executable demo
[100%] Built target demo
~/so/build> ./demo
main temp address is 0x56135a414044, value is 0
fn temp address is 0x7f170b3cb028, value is 1

我不确定这是否与 policy change 有关在 v3.4 中引入,但最相关的更改似乎是 CMP0065 .

关于c - 不同的编译方式导致同一个动态库产生不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57201517/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com