undefined reference to `libintl_gettext' with MinGW/MSYS and CMake
Asked Answered
D

1

6

I am trying to add gettext to a C++ project. It compiles and runs fine under Linux, but I get a linker error with MinGW32 in Windows 7 64 bit. I am compiling with cmake -G "MSYS Makefiles" .., because MinGW doesn't work for soe nebulous reason. I also tried ninja.

In the CMakeLists.txt, I have

find_package(Gettext REQUIRED
include_directories(${GETTEXT_INCLUDE_DIR})
if (INTL_FOUND)
    find_package(INTL REQUIRED)
    include_directories(${INTL_INCLUDE_DIR})
endif()

Which is found as

 GETTEXT_INCLUDE_DIR = C:/MinGW/msys/1.0/local/include

The project is compiling without problems, but then in the linking stage, I get the following errors:

<source file location> undefined reference to `libintl_gettext'
<source file location> undefined reference to `libintl_setlocale'
<source file location> undefined reference to `libintl_textdomain'

etc.

I had a look at https://www.gnu.org/software/gettext/FAQ.html#integrating_undefined

and added the following to CMakeLists.txt:

SET(GCC_COVERAGE_COMPILE_FLAGS "-s -O2 -lintl")
SET(GCC_COVERAGE_LINK_FLAGS    "-lintl")
SET(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}" )
SET(CMAKE_EXE_LINKER_FLAGS  "${CMAKE_EXE_LINKER_FLAGS} 

then I tried

SET(GCC_COVERAGE_COMPILE_FLAGS "-s -O2 -lintl -liconv -LC:/MinGW/msys/1.0/local/lib -IC:/MinGW/msys/1.0/local/include")
SET(GCC_COVERAGE_LINK_FLAGS    "-lintl -liconv -LC:/MinGW/msys/1.0/local/lib -IC:/MinGW/msys/1.0/local/include")

which also didn't work.

Dichroite answered 16/11, 2015 at 8:48 Comment(0)
H
4

I am using Win-builds and I am having a similar problem. I am only trying to build a Hello World program using gettext.

From what I understand, this should work but does not:

gcc -lintl.dll -D__USE_MINGW_ANSI_STDIO -Wall -Wextra -Werror -std=gnu99 -o testGettex.exe testGettex.c

The output is:

C:\Users\Julien\AppData\Local\Temp\ccAvCaR2.o:testGettex.c:(.text+0x1a): undefined reference to `libintl_setlocale'
C:\Users\Julien\AppData\Local\Temp\ccAvCaR2.o:testGettex.c:(.text+0x2b): undefined reference to `libintl_setlocale'
C:\Users\Julien\AppData\Local\Temp\ccAvCaR2.o:testGettex.c:(.text+0x3e): undefined reference to `libintl_bindtextdomain'
C:\Users\Julien\AppData\Local\Temp\ccAvCaR2.o:testGettex.c:(.text+0x4a): undefined reference to `libintl_textdomain'
C:\Users\Julien\AppData\Local\Temp\ccAvCaR2.o:testGettex.c:(.text+0x56): undefined reference to `libintl_gettext'
C:\Users\Julien\AppData\Local\Temp\ccAvCaR2.o:testGettex.c:(.text+0x5e): undefined reference to `__printf__'
d:/prog/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Julien\AppData\Local\Temp\ccAvCaR2.o: bad reloc address 0x0 in section `.pdata'
d:/prog/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status

So I tried to link directly using the .dll instead of using the .dll.a, and I could build with this command:

gcc -D__USE_MINGW_ANSI_STDIO -Wall -Wextra -Werror -std=gnu99 -o testGettex.exe D:\Prog\Win-builds\bin\libintl-8.dll testGettex.c

However, I don't know how you can tell to CMake to link directly to .dll... Maybe the best to do would be to fing MinGW's bug tracker and to explain them the problem with the file libintl.dll.a.

Edit: I just read the link you give and I saw another solution who also worked, moving the two -l options to the end of command

gcc -D__USE_MINGW_ANSI_STDIO -Wall -Wextra -Werror -std=gnu99 -o testGettex.exe testGettex.c -lintl.dll -liconv

Maybe there could be a way to tell CMake to move the two options at end of line.

Harmonica answered 31/3, 2016 at 11:22 Comment(1)
Thanks for the tip. My situation was a bit different – I am building pacman on MSYS2 (Cygwin clone) using Meson+Ninja. Here in Ninja recipe the POSIX-like libs are indicated directly, using their full path names (while WinAPI libs goes by using -l... notation). In my case I inserted this into the middle of the command line: /usr/lib/gcc/x86_64-pc-msys/11.3.0/../../../../lib/libintl.dll.a (which I copied from the other command line which succeeded). The real path /usr/lib/libintl.dll.a works too. Cheers!Veron

© 2022 - 2024 — McMap. All rights reserved.