简体   繁体   English

如何将我的 C 代码与 PCRE 库链接? (当前抛出的链接器错误。)

[英]How can I link my C code with the PCRE library? (Linker errors currently being thrown.)

The Problem问题

Note: I originally had this problem in a much larger project;注意:我最初在一个更大的项目中遇到了这个问题; so I pared the code down to the test case you see below.所以我将代码缩减为您在下面看到的测试用例。

I cannot figure out how to get the following test code to compile.我无法弄清楚如何编译以下测试代码。 Specifically, it appears as if the linker cannot find the PCRE library (see below for how PCRE was configured).具体来说,似乎 linker 找不到 PCRE 库(有关如何配置 PCRE,请参见下文)。 And this despite the explicit -L/usr/local/lib -lpcre being passed to the linker (PCRE is installed in the /usr/local directory structure).尽管显式-L/usr/local/lib -lpcre被传递给 linker (PCRE 安装在/usr/local目录结构中),但这仍然存在。

What am I doing wrong?我究竟做错了什么? :-( :-(

The console output is:控制台 output 是:

$ make
rm -f ./*.o
gcc -ansi -Wall -pedantic-errors -I/usr/local/include -g0 -O3 -static -static-libgcc -march=i686 -malign-double -m128bit-long-double -c main.c -o main.o
gcc -ansi -Wall -pedantic-errors -L/usr/local/lib -lpcre -g0 -O3 -static -static-libgcc -march=i686 -malign-double -m128bit-long-double main.o -o pcre_test_1_i686
main.o:main.c:(.text+0x100): undefined reference to `pcre_compile2'
main.o:main.c:(.text+0x12e): undefined reference to `pcre_study'
main.o:main.c:(.text+0x16e): undefined reference to `pcre_exec'
main.o:main.c:(.text+0x19f): undefined reference to `pcre_copy_substring'
collect2: ld returned 1 exit status
make: *** [all] Error 1

Relevant Files相关文件


PCRE Configuration and Compile Environment PCRE配置和编译环境

./configure --disable-shared --enable-static --disable-cpp --enable-rebuild-chartables --enable-utf8 --enable-unicode-properties --enable-newline-is-any --disable-stack-for-recursion --with-posix-malloc-threshold=2 --with-link-size=4

CC="gcc"
CFLAGS="-g0 -O3 -static -static-libgcc -march=i686 -malign-double -m128bit-long-double"
LD_RUN_PATH="/usr/local/include:/usr/local/lib"

main.c main.c

#define PCRE_STATIC  
#include <pcre.h>  
#include <stdlib.h>  
#include <errno.h>  
#include <stdio.h>  
#include <string.h>  

#define OUTPUT_SIZE 12
#define SUBSTRING_SIZE 16

int main(int argc, char *argv[]) {

  pcre *re;
  pcre_extra *extra;
  const char *input = "get food";
  const char *pattern = "^\\s*get\\s+(\\w+)\\s*$\0";
  int options = PCRE_CASELESS | PCRE_UTF8 | PCRE_UCP;
  int error_code = 0;
  int error_offset = 0;
  const char *compile_error;
  const char *study_error;
  int output[OUTPUT_SIZE];
  char substring[SUBSTRING_SIZE];
  int matched = 0;
  int is_error = 0;
  int index = 0;
  for(index = 0; index < OUTPUT_SIZE; index++) {
    output[index] = 0;
  }
  re = pcre_compile2( pattern,
                      options,
                      &error_code,
                      &compile_error,
                      &error_offset,
                      NULL );
  if(re == NULL) {
    fprintf(stderr, "PCRE regular expression error at position %d: %s", error_offset, compile_error);
    exit(EXIT_FAILURE);
  }
  if(error_code == 0) {
    extra = pcre_study( re,
                        0,
                        &study_error );
  }
  else {
    fprintf(stderr, "PCRE regular expression error at position %d: %s", error_offset, compile_error);
    extra = NULL;
  }

  matched = pcre_exec( re,
                       extra,
                       input,
                       (int)strlen(input),
                       0, /* Start at the beginning of the string */
                       0,
                       output,
                       OUTPUT_SIZE );
  if(matched > 1) {
    int status = pcre_copy_substring( input,
                                      output,
                                      matched,
                                      1,
                                      substring,
                                      SUBSTRING_SIZE );
    if(status < 0) {
      switch(status) {
        case PCRE_ERROR_NOMEMORY:
          fprintf(stderr, "PCRE substring extraction error: %s", "Buffer too small");
          break;
        case PCRE_ERROR_NOSUBSTRING:
          fprintf(stderr, "PCRE substring extraction error: %s", "Invalid substring number");
          break;
      }
      is_error = 1;
    }
  }

  printf("Capture group 1 is: '%s'\n", substring);
  if(is_error) {
    printf("There was an error with the pcre_copy_substring() function.\n");
  }

  return EXIT_SUCCESS;
}

Makefile Makefile

PACKAGE = pcre_test
VERSION = 1

# -ansi == ANSI C
# -std=iso9899:199409 == ANSI C w/ Amendment 1
# -std=c99 == ISO C99
GCC_CMD = gcc -ansi -Wall -pedantic-errors

# Generic: i686
# Intel: core2, corei7, corei7-avx
# AMD: k8-sse3, opteron-sse3, athlon64-sse3, amdfam10
ARCH = i686

RELEASE_FILE = $(PACKAGE)_$(VERSION)_$(ARCH)

GCC_OPTIONS = -g0 -O3 -static -static-libgcc -march=$(ARCH) -malign-double -m128bit-long-double
GCC_COMPILE = $(GCC_CMD) -I/usr/local/include $(GCC_OPTIONS)
GCC_LINK = $(GCC_CMD) -L/usr/local/lib -lpcre $(GCC_OPTIONS)

GCC_LINK_ALL = $(GCC_LINK) main.o

all: clean build_main
        $(GCC_LINK_ALL) -o $(RELEASE_FILE)

build_main:
        $(GCC_COMPILE) -c main.c -o main.o

clean:
        rm -f ./*.o

[EDIT] [编辑]

The Answer Has Been Found!答案已找到!

All external library flags must be listed after all of the object files.所有外部库标志必须列所有 object 文件之后。 Thus, the Makefile should have looked like this:因此, Makefile应该如下所示:

GCC_LINK = $(GCC_CMD) -L/usr/local/lib $(GCC_OPTIONS)

GCC_LINK_ALL = $(GCC_LINK) main.o -lpcre

Thank you to everyone who responded with answers.感谢所有回答的人。 :-) :-)

Is it possible the linker is finding a pre-built version of the libpcre.so? linker 是否有可能找到 libpcre.so 的预构建版本?

Try the advice from this question : "specify /usr/local/lib/libpcre.a on the compiler command line... avoid including -lpcre".试试这个问题的建议:“在编译器命令行上指定/usr/local/lib/libpcre.a ...避免包括-lpcre”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果我的 c 代码和 Linker 脚本中有一个 const 变量,并且 flash 中存在 RO 数据,我该如何修改它? - If there is a const variable in my c code and Linker script has RO data is present in flash, how can I modify it? 如何消除程序中的链接器错误? - How do I eliminate the linker errors in my program? 如何在C#代码库中调用C方法? - How can I call a C method from a library on my C# code? 如何链接到我自己的pthread库 - How can I link with my own pthread library C / cmake - 在TARGET_LINK_LIBRARIES中指定库时,如何向(未使用的)库添加链接器标志? - C/cmake - how to add a linker flag to an (unused) library when the library is specified in TARGET_LINK_LIBRARIES? 如何以编程方式获取当前可从 C/C++ 代码获得的 memory 的数量? - How can I programmatically get the amount of memory currently available from C/C++ code? 如何强制Inline :: C链接到较旧的库? - How can I force Inline::C to link to an older library? 如何将库链接到我的 C 代码并在 Rust 二进制文件中使用它? - How can I link libraries to my C code and use that in a Rust binary? 调试时如何隐藏我的C库代码 - How can I hide my C-library code while debuging 如何检查C代码使用的内存? - How do I check the memory being used by my C code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM