我正在学习在Debian 12上使用libwget.我已经安装了"wget2-dev"包,它递归地安装了"libwget0"包.我在wget2 examples的帮助下写了一个非常基本和简单的应用程序.
#include <stdio.h>
#include <stdlib.h>
#include <wget.h>
int main(int argc, char *argv[])
{
wget_http_connection_t *conn = NULL;
wget_http_response_t *resp;
// set up libwget global configuration
wget_global_init(
WGET_COOKIES_ENABLED, 1,
WGET_COOKIE_SUFFIXES, "public_suffixes.txt",
WGET_COOKIE_FILE, "cookies.txt",
WGET_COOKIE_KEEPSESSIONCOOKIES, 1,
0);
// execute an HTTP GET request and return the response
resp = wget_http_get(
WGET_HTTP_URL, "http://example.com",
WGET_HTTP_MAX_REDIRECTIONS, 5,
WGET_HTTP_CONNECTION_PTR, &conn,
0);
if (resp)
{
printf("%s", resp->body->data);
// free the response
wget_http_free_response(&resp);
}
// close connection if still open
wget_http_close(&conn);
// free resources - needed for valgrind testing
wget_global_deinit();
return EXIT_SUCCESS;
}
我的问题是,在调用wget_global_init函数时,如果我将"WGET_COOKIES_ENABLED"设置为"1",我就会得到一个分段错误.
这里出了什么问题?是因为图书馆的事,还是我做错了什么?谢谢.
编辑:我在另一个与分段故障相关的question个问题上try 了答案.如果我输入
gdb ./MyApp
我得到
(gdb) run
Starting program: MyApp
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Program received signal SIGSEGV, Segmentation fault.
___pthread_mutex_lock (mutex=0x0) at ./nptl/pthread_mutex_lock.c:80
80 ./nptl/pthread_mutex_lock.c: No such file or directory.
那是什么意思?我怎么才能解决它呢?
编辑2:如果我键入
valgrind --leak-check=full ./MyApp
我得到
==42170== Invalid read of size 4
==42170== at 0x49541C0: pthread_mutex_lock@@GLIBC_2.2.5 (pthread_mutex_lock.c:80)
==42170== by 0x487A7EC: wget_cookie_create_request_header (in /usr/lib/libwget.so.0.0.0)
==42170== by 0x4895153: wget_http_get (in /usr/lib/libwget.so.0.0.0)
==42170== by 0x1091EE: main (main.c:23)
==42170== Address 0x10 is not stack'd, malloc'd or (recently) free'd
==42170==
==42170==
==42170== Process terminating with default action of signal 11 (SIGSEGV)
==42170== Access not within mapped region at address 0x10
==42170== at 0x49541C0: pthread_mutex_lock@@GLIBC_2.2.5 (pthread_mutex_lock.c:80)
==42170== by 0x487A7EC: wget_cookie_create_request_header (in /usr/lib/libwget.so.0.0.0)
==42170== by 0x4895153: wget_http_get (in /usr/lib/libwget.so.0.0.0)
==42170== by 0x1091EE: main (main.c:23)
这是一个错误,还是我没有正确地使用库?
编辑3:在Olaf Dietsche的注释之后,我在Eclipse CDT中添加了-pthread
来构建设置
Invoking: GCC C Compiler
gcc -pthread -I/usr/include -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/main.d" -MT"src/main.o" -o "src/main.o" "../src/main.c"
Invoking: GCC C Linker
gcc -pthread -L/usr/lib -o "MyApp" ./src/main.o -lwget
当我运行程序时,我仍然收到相同的错误.
编辑4:在Olaf Dietsche条 comments 之后,我也添加了-pthread
条GCC链接选项.而且我仍然有分段故障.