【发布时间】:2018-04-12 14:38:05
【问题描述】:
[g++4.8.2, pcre2-10.31, linux] [c++ ~newbie] 我正在尝试学习如何使用 c++ 程序对 pcre2 的一些简单调用。
#include <string>
#define PCRE2_CODE_UNIT_WIDTH 8
#include "pcre2.h"
int main()
{
std::string xpattern = "(\\+|-)?[[:digit:]]+";
int errorcode;
PCRE2_SIZE *erroroffset = NULL;
pcre2_code *code = pcre2_compile((const unsigned char *)xpattern.c_str(), PCRE2_ZERO_TERMINATED, 0, &errorcode, erroroffset, NULL);
}
g++ -I/scratch_b/qa/tmp/pcre/include -g -Wall -o simple simpleregex2.cpp
simpleregex2.cpp: In function ‘int main()’:
simpleregex2.cpp:9:15: warning: unused variable ‘code’ [-Wunused-variable]
pcre2_code *code = pcre2_compile((const unsigned char *)xpattern.c_str(), PCRE2_ZERO_TERMINATED, 0, &errorcode, erroroffset, NULL);
^
simpleregex2.cpp:9: error: undefined reference to 'pcre2_compile_8'
collect2: error: ld returned 1 exit status
谢谢,我修复了指针问题,仍然得到'未定义的引用'...?
【问题讨论】:
-
根据this API documentation
pcre2_compile返回一个指针,而不是一个副本 -
虽然您显示的代码是一个不错的Minimal, Complete, and Verifiable Example,但它与您显示的错误消息不匹配。该代码的 sn-p 中没有 15 行,并且错误在第 15 行。始终从您实际显示的代码中显示错误。
-
请在发帖前阅读 API。像这样的 C API 通常适用于不透明结构,因此您只能通过指针而不是值来使用它们。因此是“不完整类型”(在您尝试将不兼容类型的表达式分配给该不完整类型之前)。我投票决定关闭它,因为 (A) 显示的代码不完整和 (B) 由简单的印刷错误引起,即缺少星号。