Home > Back-end >  MinGW undefined reference libcurl
MinGW undefined reference libcurl

Time:04-17

So i have written an app in c to download mp3s from the web using a list. It uses libcurl to download them.

I am on linux. Compiling with

 g   main.cpp -lcurl -o word2mp3 

works fine. I need an windows executable, but running x86_64-w64-mingw32-g main.cpp -o wordtest returns the

undefined reference to `__imp_curl_easy_init'

followed by the other curl functions. Adding -lcurl to the command also gives an error saying that curl isn't found.

I've tried and searched everywhere, no luck, I'm a beginner.

Link to my github repo: https://github.com/sharpclone/Word2Mp3

CodePudding user response:

You need to compile libcurl with MinGW, or find a precompiled one. The one you installed to compile your app on Linux was compiled with a Linux compiler, and wouldn't work with MinGW.

MSYS2 repos have a bunch of precompiled libraries for different flavors of MinGW.

I've made a script to automatically download those libraries, since their package manager only works on Windows.

git clone https://github.com/holyblackcat/quasi-msys2
cd quasi-msys2
make install _gcc _curl
env/shell.sh

cd ..
git clone https://github.com/sharpclone/Word2Mp3
cd Word2Mp3/
win-clang   main.cpp -lcurl -o word2mp3

This doesn't rely on an external MinGW installation, and only requires Clang (and LLD) to be installed (a regular Clang for Linux, unlike GCC you don't need to install a special version of it to cross-compile).

Or, if you prefer your existing compiler, you can stop at make install _curl, then manually specify the path to the installed library, normally -Lquasi-msys2/root/mingw64/lib. You just need to make sure the MSYS2 repo you're using matches your compiler.

CodePudding user response:

To fix the undefined reference warning, you need to link to the required library, so the -lcurl is correct and required.

If the compiler cannot find the library, you need to tell him where to find it, that can be done by passing -L followed by the path to the library (that would be the path to the directory where libcurl.dll.a is in).

The order of the parameters can also be relevant, I think the correct order would be -L <path> -lcurl.

  • Related