1

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

2 Answers2

2

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.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
  • I think i'm stupid , Im on arch , i used pacman -S clang. I Followed and isntalled the _gcc and _curl, now it says that theres no such command win-clang++, simply clang works. MinGW just refuses to do something – Mihai Cazac Apr 16 '22 at 12:35
  • You need to do `env/shell.sh` first. It opens a shell where `win-clang++` is added to the PATH. – HolyBlackCat Apr 16 '22 at 12:36
  • Ok another error code: `/home/mihai/gits/quasi-msys2/env/wrappers/win-clang++: line 2: : command not found ` The file content: `#!/bin/sh "$WIN_NATIVE_CLANG_CXX" $WIN_CLANG_FLAGS "$@" ` – Mihai Cazac Apr 16 '22 at 12:44
  • @MihaiCazac Did you run `env/shell.sh`, or did you manually specify full path to `win-clang++`? If `env/shell.sh` doesn't help (which is weird), try doing `export WIN_NATIVE_CLANG_CXX=clang++`... – HolyBlackCat Apr 16 '22 at 12:45
  • OK finally it worked now , one last caveat that running it in windows returns this app can't run on your pc, yes i run env/shell – Mihai Cazac Apr 16 '22 at 12:49
  • @MihaiCazac I have a strong suspicion that you're not running `env/shell.sh`, and end up producing a Linux executable instead of a Windows one ("can't run on your pc" supports that). Can you explain in detail what commands you used? – HolyBlackCat Apr 16 '22 at 12:52
  • The image: [https://imgur.com/a/AGdVMLE](https://imgur.com/a/AGdVMLE) _italic_ **bold**. My exact steps: After make install i exported that win native then i followed the exact steps: `[mihai@mihai-arch quasi-msys2]$ cd .. [mihai@mihai-arch gits]$ cd Word2Mp3/ [mihai@mihai-arch Word2Mp3]$ win-clang++ main.cpp -lcurl -o word2mp3v3.exe ` – Mihai Cazac Apr 16 '22 at 12:55
  • Also can you share the output from `env/shell.sh`? – HolyBlackCat Apr 16 '22 at 12:58
  • Note i did it all in env/shell.sh . `--- binfmt.mk `binfmt_misc` mounted? YES `binfmt_misc` enabled? YES Executable format registered? YES Executable format enabled? YES --- fakebin.mk Makefile:167: *** Aborted. Stop. Nothing to do. --- vars.sh The environment variables are already set. Restart the shell and re-run this script to update them. ` – Mihai Cazac Apr 16 '22 at 13:01
  • @MihaiCazac Close the terminal, open a new one, run `env/shell.sh` again and show what it prints. Don't do `export WIN_NATIVE_CLANG_CXX`, just try running `win-clang++ ...` in it again. – HolyBlackCat Apr 16 '22 at 13:02
  • Guessing a compiler... To override, set `MINGW_CC` and `MINGW_CXX` and restart. Trying native Clang... Guessed Clang version suffix: WIN_CLANG_VER = 13 You can override it by setting it to a number or to `NONE` for no suffix. which: no clang++-13 in (/home/mihai/.emacs.d/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/var/lib/snapd/snap/bin:/usr/lib/jvm/java-8-j9//bin:/snap/bin) Fail. Trying MSYS2 Clang... (not recommended) Fail. Trying MSYS2 GCC... Success. CC = gcc CXX = g++ – Mihai Cazac Apr 16 '22 at 13:04
  • When running win-clang++ = command not found – Mihai Cazac Apr 16 '22 at 13:05
  • It seems that it defaults the CC to gcc and Cxx to g++, the msys2 Clang fails – Mihai Cazac Apr 16 '22 at 13:08
  • @MihaiCazac For some reason you have `clang-13` installed but not `clang++-13`, which is weird (and messes up compiler detection). Restart the terminal, run `export WIN_CLANG_VER=NONE`, then run `env/shell.sh` and try `win-clang++` again. – HolyBlackCat Apr 16 '22 at 13:09
  • I've fixed the script to handle this automatically, even though I'm not sure why it's needed. – HolyBlackCat Apr 16 '22 at 13:10
  • Wait theres more).It never ends. Now i get invalid linker name in argument : '-fuse-ld=lld" – Mihai Cazac Apr 16 '22 at 13:14
  • @MihaiCazac You need to install `lld`, as the readme says. :) – HolyBlackCat Apr 16 '22 at 13:14
  • The link to everything used (pastebin) [https://pastebin.com/atTpJX50](https://pastebin.com/atTpJX50) _italic_ **bold** – Mihai Cazac Apr 16 '22 at 13:15
  • Finally, thanks it worked , i still get some errors on windows like libstdc++ not found but i think i tortured you enough ,Thanks! :) – Mihai Cazac Apr 16 '22 at 13:19
  • @MihaiCazac No problem. You need to copy those dlls from `quasi-msys2/mingw64/root/mingw64/bin/` and place them next to your executable. Also see the section on the DLL debugging [here](https://stackoverflow.com/a/64396980/2752075) (that's for a different library, but should work equally well here too). – HolyBlackCat Apr 16 '22 at 13:21
1

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.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Thanks for answering, but the problem is still the same...I tried to spun up a windows vm and after installing libcurl it's the same problem. The code i used: `.\g++.exe C:\Users\Mihai\Desktop\Word2Mp3-main\main.cpp -o C:\Users\Mihai\Desktop\Word2Mp3-main\word2mp3 -LC:\ProgramData\chocolatey\lib\curl\tools\curl-7.82.0-win64-mingw\lib -lcurl` – Mihai Cazac Apr 16 '22 at 11:57