以下範例取自《Unix 環境高級編程》的錯誤處理例程,完整走一遍靜態庫與動態庫的編譯、連結與呼叫流程。
#ifndef _LIBERR_H #define _LIBERR_H #include <stdarg.h> #define MAXLINELEN 4096 void err_ret(const char *fmt, ...); void err_quit(const char *fmt, ...); void log_ret (char *logfile, const char *fmt, ...); void log_quit(char *logfile, const char *fmt, ...); void err_prn(const char *fmt, va_list ap, char *logfile); #endif
#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "liberr.h"
void err_ret(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_prn(fmt, ap, NULL);
va_end(ap);
return;
}
void err_quit(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_prn(fmt, ap, NULL);
va_end(ap);
exit(EXIT_FAILURE);
}
void log_ret(char *logfile, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_prn(fmt, ap, logfile);
va_end(ap);
return;
}
void log_quit(char *logfile, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_prn(fmt, ap, logfile);
va_end(ap);
exit(EXIT_FAILURE);
}
extern void err_prn(const char *fmt, va_list ap, char *logfile)
{
int save_err;
char buf[MAXLINELEN];
FILE *plf;
save_err = errno;
vsprintf(buf, fmt, ap);
sprintf(buf + strlen(buf), ":%s", strerror(save_err));
strcat(buf, "\n");
fflush(stdout);
if (logfile != NULL)
if ((plf = fopen(logfile, "a")) != NULL) {
fputs(buf, plf);
fclose(plf);
} else
fputs("failed to open log file\n", stderr);
else
fputs(buf, stderr);
fflush(NULL);
return;
}
gcc -c liberr.c -o liberr.o ar rcs liberr.a liberr.o
#include <stdio.h>
#include <stdlib.h>
#include "liberr.h"
#define ERR_QUIT_SKIP 1
#define LOG_QUIT_SKIP 1
int main(void)
{
FILE *pf;
puts("Testing err_ret");
if ((pf = fopen("foo", "r")) == NULL) {
err_ret("%s %s", "err_ret:", "failed to open foo");
}
puts("Testing log_ret");
if ((pf = fopen("foo", "r")) == NULL) {
log_ret("errtest.log", "%s %s", "log_ret:", "failed to open foo");
}
exit(EXIT_SUCCESS);
}
這裡把原本的 open() 改成了 fopen(),正好對應原文明確記錄的那個警告:assignment makes pointer from integer without a cast——原因是漏了函式宣告,編譯器把 open 當成回傳 int 的函式,再把 int 硬塞給指標。
gcc errtest.c -o errtest -L. -lerr --static /usr/bin/ld: cannot find -lc collect2: ld returned 1 exit status
解法是補上靜態版本的 C 標準庫:
yum install glibc-static
安裝後重跑即可通過。
gcc -fPIC -g -c liberr.c -o liberr.o gcc -g -shared -Wl,soname,liberr.so -o liberr.so.1.0.0 liberr.o -lc
-fPIC 產生位置無關程式碼,是動態庫的前提;-Wl,soname,... 把 soname 寫進庫中,管理版本時很重要。實測可看出靜態連結的執行檔比動態連結大了將近一百倍,這正是靜態連結最直接的代價。
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
int main(void)
{
void *handle;
void (*errfcn)(const char *fmt, ...); // 函式指標,指向 err_ret
const char *errmsg;
FILE *pf;
handle = dlopen("./liberr.so", RTLD_NOW);
if (handle == NULL) {
fprintf(stderr, "Failed to load liberr.so: %s\n", dlerror());
exit(EXIT_FAILURE);
}
dlerror(); // 清除錯誤狀態
errfcn = dlsym(handle, "err_ret");
if ((errmsg = dlerror()) != NULL) {
fprintf(stderr, "Didn't find err_ret: %s\n", errmsg);
exit(EXIT_FAILURE);
}
if ((pf = fopen("foobar", "r")) == NULL) {
errfcn("Couldn't open foobar");
}
dlclose(handle);
exit(EXIT_FAILURE);
}
編譯時記得連結 libdl:
gcc -g dltest.c -Wall -ldl -o dltest
原文提到一個很典型的除錯經驗:初次執行完全沒有輸出,逐步排查後發現是那段 fopen 判斷的程式碼漏貼了,補上後才正常。這也說明「程式跑完什麼都沒發生」時,先懷疑主流程是否真的被执行,往往比埋頭看細節有效。