简体   繁体   English

TDM-GCC引发错误:对我从未调用的方法的未定义引用

[英]TDM-GCC raises error: Undefined reference to a method I never called

This is with the compiler that comes with Dev-C++ when run on Windows 7, "TDM-GCC 4.9.2 64-bit Release". 这是在Windows 7上运行时带有Dev-C ++的编译器,“TDM-GCC 4.9.2 64位版本”。 The same thing happens with the 64-bit version as well. 同样的事情也发生在64位版本上。 The "add the following flags when calling the linker" in "Compiler Options" is set to -static-libgcc -lws2_32 , and "add the following flags when calling the compiler" is std=c++11 . “编译器选项”中的“在调用链接器时添加以下标志”设置为-static-libgcc -lws2_32 ,并且“调用编译器时添加以下标志”为std=c++11

Unfortunately, I can't get much more information than that because this is a very, very locked down computer. 不幸的是,我无法获得更多信息,因为这是一台非常非常锁定的计算机。

Once I get home, I'll test it there and edit in any new information. 一旦我回到家,我会在那里测试并编辑任何新信息。

A test file: 测试文件:

#include <windows.h>
#include <psapi.h>
#include <iostream>

int main() {
    HANDLE h = GetCurrentProcess();
    TCHAR name[100];
    GetProcessImageFileName(h, name, 100);
    std::cout << "Name is " << std::endl;
}

When compiled, it gives the following error: 编译时,它会出现以下错误:

C:\Users\[REDACTED]\AppData\Local\Temp\ccQz3L9P.o:test.cpp:(.text+0x2f): undefined reference to `GetProcessImageFileNameA'

As best as I can tell, it's calling GetProcessImageFileNameA instead of GetProcessImageFileName because of a #define in psapi.h : 据我所知,由于psapi.h中的#define ,它调用GetProcessImageFileNameA而不是GetProcessImageFileName

#define GetProcessImageFileName __MINGW_NAME_AW(GetProcessImageFileName)

__MINGW_NAME_AW(func) is #define d in _mingw_unicode.h as func##A , which if I understand it correctly is where the new method name is coming from. __MINGW_NAME_AW(func)_mingw_unicode.h #define d为func##A ,如果我正确理解它是新方法名称的来源。

Back in psapi.h , DWORD WINAPI GetProcessImageFileNameA(HANDLE, LPSTR, DWORD); 回到psapi.hDWORD WINAPI GetProcessImageFileNameA(HANDLE, LPSTR, DWORD); is declared, but I haven't found its definition anywhere, and Dev-C++ can't either. 声明,但我没有在任何地方找到它的定义,Dev-C ++也不能。 I'm fairly certain that's the problem. 我很确定这是问题所在。

How can I fix it? 我该如何解决? I'm open to anything, up to and including switching compilers or IDEs, as long as I can put whatever I use on a flash drive and carry it around with as little annoyance as possible, and that the end result doesn't require admin privileges to run. 我对任何事情都持开放态度,包括切换编译器或IDE,只要我可以将我在闪存驱动器上使用的任何东西放在尽可能小的烦恼中,并且最终结果不需要管理员要运行的权限。

GetProcessImageFileName is a windows API function and you need to link in the correct library to get to it. GetProcessImageFileName是一个Windows API函数,您需要链接到正确的库中才能访问它。

It is defined as a macro to be either GetProcessImageFileNameA or GetProcessImageFileNameW depending whether you are compiling "wide" or "narrow". 它被定义为一个宏,可以是GetProcessImageFileNameAGetProcessImageFileNameW具体取决于您是在编译“wide”还是“narrow”。

The Win32 API has Unicode and ascii versions of each function (at least the functions that take strings). Win32 API具有每个函数的Unicode和ascii版本(至少是带字符串的函数)。 In general, the ascii version converts the characters to Unicode and calls the other implantation internally. 通常,ascii版本将字符转换为Unicode并在内部调用其他植入。

you need to link Psapi.lib or Kernel32.lib into your project. 您需要将Psapi.libKernel32.lib链接到您的项目中。 I am not sure about your compiler but in VC++ you add #pragma comment(lib, "Psapi.Lib") to your code or add Psapi.lib in additional libraries of project properties. 我不确定您的编译器,但在VC ++中,您将#pragma comment(lib, "Psapi.Lib")添加到您的代码中,或者在其他项目属性库中添加Psapi.lib

Sam 山姆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM