G++ link 针对 Cygwin 中的 psapi

G++ link against psapi in Cygwin

我有一个用 Visual C++ 构建的 C++ 项目。在我使用的项目中:

#pragma comment(lib, "psapi")

为了link反对psapi

不过,G++ 似乎不支持这种语法。据我了解,您必须传递带有库名称的 -l 标志才能 link 反对它。

我尝试了 -lpsapi.lib-lpsapi
但是 gcc 找不到它。
所以我搜索了在哪里可以找到它,显然它在 /lib/w32api/libpsapi.a 中。所以我试了-llibpsapi.a-llibpsapi,还是找不到。
所以我尝试使用 -L 标志添加路径 -L/lib/w32api,但它仍然找不到它。
然后我尝试添加两个环境变量而不是 -L 标志:

export LIBRARY_PATH=/lib/w32api
export LD_LIBRARY_PATH=/lib/w32api

但是还是不行。

错误信息是:

/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/../../../../x86_64-pc-cygwin/bin/ld: cannot find -llibpsapi
collect2: error: ld returned 1 exit status

我最后一次尝试是:

g++ -llibpsapi -o Example.exe Stuff.cpp Example.cpp -static

如果我完全不使用 -l 标志,那么顺便说一句,我会得到这些错误:

/tmp/cctDMx8f.o:Stuff.cpp:(.text+0x139): undefined reference to `EnumProcessModules'
/tmp/cctDMx8f.o:Stuff.cpp:(.text+0x139): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `EnumProcessModules'
/tmp/cctDMx8f.o:Stuff.cpp:(.text+0x181): undefined reference to `EnumProcessModules'
/tmp/cctDMx8f.o:Stuff.cpp:(.text+0x181): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `EnumProcessModules'
/tmp/cctDMx8f.o:Stuff.cpp:(.text+0x1db): undefined reference to `GetModuleBaseNameA'
/tmp/cctDMx8f.o:Stuff.cpp:(.text+0x1db): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `GetModuleBaseNameA'
collect2: error: ld returned 1 exit status

我明白了。首先,我必须使用 -lpsapi,第二个重要部分是我没有将它插入到 -o 标志之前。以下工作正常:

g++ -o Example.exe Stuff.cpp Example.cpp -static -lpsapi