Home > Enterprise >  Why do I get an undefined reference although everything seems alright ? (C Mingw)
Why do I get an undefined reference although everything seems alright ? (C Mingw)

Time:06-17

My problem is fairly trivial and simple, I am trying to write a packer and to do so I need to parse PE files, so I'm trying to use the C pe-parse library.

I built it following the instructions and I'm now trying to link it to my simple main.cpp file:

#include <pe-parse/parse.h>

int main(int ac, char **av)
{
    peparse::parsed_pe *p = peparse::ParsePEFromFile(av[0]);
    return 0;
}

Here is my file structure:

.
├── src
│      main.cpp
├── lib
│      pe-parse.lib
├── bin
│      pe-parse.dll
└── include
    └ pe-parse
         nt-headers.h
         parse.h
         to_string.h

MinGW is indeed x64 (x86_64-w64-mingw32) and my libraries also (pei-x86-64 for both pe-parse.dll and pe-parse.lib)

When I run

g   -Wall -Wextra .\src\main.cpp -I.\include\ -L.\bin\ -lpe-parse

from root, I get the following linking error:

C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe:
C:\Users\gz\AppData\Local\Temp\ccxml3rK.o:packer.cpp:(.text 0x1f): undefined reference to `peparse::ParsePEFromFile(char const*)'
collect2.exe: error: ld returned 1 exit status

When I run nm on pe-parse.lib I am able to find the symbol. pe-parse.dll does not contain any, and I tried to to replace -L.\bin\ with -L.\lib\

Any ideas ? I believe the .lib is an import library that has to be linked with the .dll, but I can't find a way to.

Thank you.

CodePudding user response:

You have a library produced by MSVC and you are trying to use g to link with it.

Microsoft C compiler is not compatible with g . Objects produced by one of them cannot use objects compiled by the other. They use vastly different ABIs and different standard library implementations.

Your only option is to recompile everything with one compiler.

  • Related