You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在MSYS2环境下的MinGW64中去除动态库的cyg前缀

Fixing the "cyg" Prefix Automatically Added to Dynamic Libraries in MSYS2 MinGW64

Hey there! I’ve dealt with this exact problem plenty of times when building DLLs with MSYS2’s MinGW64 toolchain. That annoying cyg prefix usually pops up because you’re accidentally using the Cygwin-style linker/tools instead of the native MinGW64 ones. Here are the most reliable fixes:

1. Use the Correct MinGW64 Terminal

First things first—don’t launch the plain MSYS2 MSYS terminal. Always open the MSYS2 MinGW 64-bit terminal from your start menu. This terminal automatically sets up your environment variables to prioritize MinGW64’s native tools (gcc, ld, etc.), which won’t add the cyg prefix.

If you’re already in a terminal and don’t want to restart, run this to switch to the MinGW64 environment:

source /mingw64/msys2_shell.cmd -mingw64

Recompile after running this, and the prefix should be gone.

2. Explicitly Specify Linker Flags

If your environment is correct but you still see the prefix, force the linker to generate the DLL name you want. When compiling your dynamic library, use a command like this:

gcc -shared -o libmylib.dll mylib.c -Wl,--out-implib=libmylib.a -Wl,--kill-at

The -Wl,--out-implib flag explicitly sets the import library name, and MinGW64’s linker won’t add the cyg prefix by default. Also, double-check that you aren’t using the -mcygwin compiler flag anywhere—it forces Cygwin-style libraries, so remove it if present.

3. Verify Your PATH Environment Variable

Sometimes your PATH has the MSYS bin directory listed before MinGW64’s, so the system picks Cygwin tools first. Check which gcc you’re using with:

which gcc

If the output is /usr/bin/gcc, that’s the Cygwin version. The correct path should be /mingw64/bin/gcc.

Fix the PATH by putting MinGW64 first:

export PATH=/mingw64/bin:$PATH

Then recompile, and you’ll get the native MinGW DLL.

4. Configure CMake Properly (If Using It)

If you’re building with CMake, make sure you’re targeting the MinGW64 toolchain. Add these lines to your CMakeLists.txt:

set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_C_COMPILER /mingw64/bin/gcc)
set(CMAKE_CXX_COMPILER /mingw64/bin/g++)
set(CMAKE_RC_COMPILER /mingw64/bin/windres)

Or when running CMake, use the MinGW generator directly:

cmake -G "MinGW Makefiles" ..

This ensures CMake uses the right tools and avoids generating Cygwin-style libraries.

At the end of the day, the key is to stick to the native MinGW64 toolchain instead of Cygwin’s. Get that right, and your DLLs will be standard Windows-style without the cyg prefix.

内容的提问来源于stack exchange,提问作者Aleksandr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 00:59:05