c - 在 C 语言中使用 GEOS API 函数返回分段错误
问题描述
我正在尝试使用我的 C 程序中的一些 libgeos ( https://github.com/libgeos/geos ) 函数。下面是我的代码。
#include <stdio.h>
#include <geos_c.h>
int main() {
GEOSContextHandle_t ctx = GEOS_init_r();
GEOSGeometry *inputGeom = GEOSGeomFromWKT("MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)), ((15 5, 40 10, 10 20, 5 10, 15 5)))");
printf("%s\n", GEOSGeomToWKT(inputGeom));
GEOS_finish_r(ctx);
return 0;
}
但问题是我在运行二进制文件时遇到了分段错误。下面是我用来编译程序的 gcc 命令。
gcc -g -Wall geos.c -o geos -lgeos_c
请参阅下面的 gdb 输出。
(gdb) run
Starting program: /home/srimal/proj/src/geos/geos
warning: the debug information found in "/lib64/ld-2.27.so" does not match "/lib64/ld-linux-x86-64.so.2" (CRC mismatch).
Program received signal SIGSEGV, Segmentation fault.
__strlen_avx2 () at ../sysdeps/x86_64/multiarch/strlen-avx2.S:62
62 ../sysdeps/x86_64/multiarch/strlen-avx2.S: No such file or directory.
如果有人可以帮助我解决此问题,我将不胜感激。
解决方案
解决了这个问题。我应该使用initGEOS
和finishGEOS
功能。以下是经过一些改进的正确答案。
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <geos_c.h>
void notice(const char *fmt, ...) {
va_list ap;
fprintf( stdout, "NOTICE: ");
va_start (ap, fmt);
vfprintf( stdout, fmt, ap);
va_end(ap);
fprintf( stdout, "\n" );
}
void log_and_exit(const char *fmt, ...) {
va_list ap;
fprintf( stdout, "ERROR: ");
va_start (ap, fmt);
vfprintf( stdout, fmt, ap);
va_end(ap);
fprintf( stdout, "\n" );
exit(1);
}
int main() {
initGEOS(notice, log_and_exit);
GEOSGeometry *inputGeom = GEOSGeomFromWKT("POINT (30 10)");
if (inputGeom == NULL) {
fprintf(stderr, "NULL GEOM");
return 0;
}
printf("%s\n", GEOSGeomToWKT(inputGeom));
finishGEOS();
return 0;
}
推荐阅读
- javascript - UnhandledPromiseRejectionWarning,discord.js javascrpt
- javascript - 在本机反应中使用 FlatList 组件显示多个图像
- pandas - 当你想替换'Ã?'时,pandas 的 str.replace() 函数不起作用 通过另一个字符串,如何解决问题?
- oracle - sqlplus :: ORA-01078: 处理系统参数失败
- python - 通电与列表一起生成,但不会重生
- r - 将excel工作表导入列表并将工作表名称添加到列表中的功能
- reactjs - 如何在 TypeScript 中为 css 模块设置汇总
- html - BR 的 innerText 中的额外换行符后跟 DIV
- java - SecureRandom 的确定性 RNG
- c# - 实体框架通用插入方法将 Guid 分配给主键