我正在尝试在 Debian 12 上学习使用 libwget。我已经安装了软件包“wget2-dev”,它递归地安装了“libwget0”软件包。我在 wget2 示例的帮助下编写了一个非常基本且简单的应用程序。
#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, 0,
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”,则会出现分段错误。
这里有什么问题吗?这是因为图书馆的原因还是我做错了什么?谢谢。
编辑:我在另一个与分段错误相关的问题中尝试了答案。如果我输入
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 发表评论后,我将
-pthread
添加到 Eclipse CDT 中的构建设置
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发表另一条评论后,我也将
-pthread
添加到 GCC 链接器选项中。我仍然遇到分段错误。
我想你想指定
WGET_COOKIE_FILE
,它也设置WGET_COOKIES_ENABLED
。
这是库中的一个错误(d51398947e150e930299f3c9657aaf6204489f4d),其中
wget_global_get_ptr()
返回&_config.cookie_db
,但调用者期望_config.cookie_db
。