我正在尝试使用 safec lib 函数但面临链接问题

Posted

技术标签:

【中文标题】我正在尝试使用 safec lib 函数但面临链接问题【英文标题】:I am trying to use safec lib function's but facing linking issue 【发布时间】:2020-06-22 10:38:47 【问题描述】:
#include <stdio.h>
#include <string.h>

int main()

        char * ch = "THISISsometest";
        char out[10] ;

        strcpy_s(out,ch);

        printf(" out : %s  its len is %d \n ", out , strlen(out));
        return 0;

尝试从 git 安装 lib 源代码 https://github.com/rurban/safeclib.git

root@DESKTOP-RUVNU9H:~/sample_C_safe# ls -l
total 3
-rw-r--r-- 1 root root  194 Jun 22 16:00 sscanf.c
-rw-r--r-- 1 root root 2008 Jun 22 16:01 sscanf.o
root@DESKTOP-RUVNU9H:~/sample_C_safe# gcc -o out.exe sscanf.o -lsafec-3.5.1
/usr/bin/ld: sscanf.o: in function `main':
sscanf.c:(.text+0x3a): undefined reference to `strcpy_s'
collect2: error: ld returned 1 exit status
root@DESKTOP-RUVNU9H:~/sample_C_safe#
root@DESKTOP-RUVNU9H:~/sample_C_safe# echo $LD_LIBRARY_PATH
:/usr/lib:/usr/local/lib
root@DESKTOP-RUVNU9H:~/sample_C_safe# ls -l /usr/local/lib/
total 932
-rw-r--r-- 1 root root  625344 Jun 22 13:14 libsafec-3.5.1.a
-rwxr-xr-x 1 root root     981 Jun 22 13:14 libsafec-3.5.1.la
lrwxrwxrwx 1 root root      23 Jun 22 13:14 libsafec-3.5.1.so -> libsafec-3.5.1.so.3.0.6
lrwxrwxrwx 1 root root      23 Jun 22 13:14 libsafec-3.5.1.so.3 -> libsafec-3.5.1.so.3.0.6
-rwxr-xr-x 1 root root  322080 Jun 22 13:14 libsafec-3.5.1.so.3.0.6
drwxr-xr-x 1 root root    4096 Jun 22 13:14 pkgconfig
drwxrwxr-x 1 root staff   4096 Jun 22 12:56 python2.7
drwxrwsr-x 1 root staff   4096 Apr 23 12:10 python3.8
root@DESKTOP-RUVNU9H:~/sample_C_safe#
root@DESKTOP-RUVNU9H:~/sample_C_safe#
root@DESKTOP-RUVNU9H:~/sample_C_safe#
root@DESKTOP-RUVNU9H:~/sample_C_safe# ldconfig -p | grep -i safec
        libsafec-3.5.1.so.3 (libc6,x86-64) => /usr/local/lib/libsafec-3.5.1.so.3
        libsafec-3.5.1.so (libc6,x86-64) => /usr/local/lib/libsafec-3.5.1.so
root@DESKTOP-RUVNU9H:~/sample_C_safe#

所以...这表明 ld 能够加载我们共享的 obj 文件

root@DESKTOP-RUVNU9H:~/sample_C_safe# nm -D /usr/local/lib/libsafec-3.5.1.so  | grep -i printf_s
0000000000010870 T _snwprintf_s_chk
00000000000083d0 T _sprintf_s_chk
000000000000f2c0 T _swprintf_s_chk
0000000000009b90 T _vsnprintf_s_chk
0000000000010cd0 T _vsnwprintf_s_chk
0000000000009930 T _vsprintf_s_chk
000000000000ef70 T _vswprintf_s_chk
000000000000ab00 T fprintf_s
000000000000fba0 T fwprintf_s
000000000000a900 T printf_s
000000000000ae50 T vfprintf_s
000000000000f6d0 T vfwprintf_s
000000000000afe0 T vprintf_s
000000000000f860 T vwprintf_s
000000000000f9b0 T wprintf_s
root@DESKTOP-RUVNU9H:~/sample_C_safe#

并且 libsafec-3.5.1.so 包含 printf_s 。 但我仍然收到错误

/usr/bin/ld: /tmp/ccUBo9E9.o: 在函数main': check2.c:(.text+0x2b): undefined reference to printf_s' collect2:错误:ld 返回 1 个退出状态

【问题讨论】:

您错误地调用了strcpy_s。它需要 3 个参数,其中一个是长度。 你包含什么头文件?看起来strcpy_s 被实现为您似乎缺少的宏。 【参考方案1】:
#include <stdio.h>
#include <string.h>
#include <safe_str_lib.h>


int main()

        char * ch = "THISISTEST2";
        char out[10] ;

        strcpy_s(out,10,ch);

        printf(" out : %s  its len is %d \n ", out , strlen(out));


        return 0;



root@DESKTOP-RUVNU9H:~/sample_C_safe#
root@DESKTOP-RUVNU9H:~/sample_C_safe# gcc sscanf.c -I /usr/lib/safeclib/include/ -lsafec-3.5.1
root@DESKTOP-RUVNU9H:~/sample_C_safe#
root@DESKTOP-RUVNU9H:~/sample_C_safe#
root@DESKTOP-RUVNU9H:~/sample_C_safe#
root@DESKTOP-RUVNU9H:~/sample_C_safe# ./a.out
 out :   its len is 0
 root@DESKTOP-RUVNU9H:~/sample_C_safe#

Now the input to < 10 bits size 

root@DESKTOP-RUVNU9H:~/sample_C_safe# vim sscanf.c

#include <stdio.h>
#include <string.h>
#include <safe_str_lib.h>


int main()

        char * ch = "THISISTE";
        char out[10] ;

        strcpy_s(out,10,ch);

        printf(" out : %s  its len is %d \n ", out , strlen(out));


        return 0;


root@DESKTOP-RUVNU9H:~/sample_C_safe# gcc sscanf.c -I /usr/lib/safeclib/include/ -lsafec-3.5.1
root@DESKTOP-RUVNU9H:~/sample_C_safe#
root@DESKTOP-RUVNU9H:~/sample_C_safe#
root@DESKTOP-RUVNU9H:~/sample_C_safe# ./a.out
 out : THISISTE  its len is 8
 root@DESKTOP-RUVNU9H:~/sample_C_safe# 

【讨论】:

以上是关于我正在尝试使用 safec lib 函数但面临链接问题的主要内容,如果未能解决你的问题,请参考以下文章

CUDA 链接错误(Lib 到 Dll)

与 Makefile 混淆似乎链接但我无法调用的库

.lib 和 .a 文件有啥区别?

如何 p/invoke 调用函数仅使用但 C++ 需要 .a/.lib/.o 文件?

Borland C++ builder 6 链接器错误

无法将 ncurses 与 Qt 链接