简体   繁体   English

生成时Windows C ++ __imp参考错误

[英]Windows C++ __imp reference error when building

Environment: 环境:

  • Windows Server 2012 R2 Windows Server 2012 R2
  • Eclipse Luna 蚀月神
  • cygwin cygwin的
  • MinGW MinGW的

I am building a C++ program that queries against Active Directory using LDAP (similar to this MSDN page ). 我正在构建一个使用LDAP针对Active Directory进行查询的C ++程序(类似于此MSDN页面 )。 I have the following code sample from the program: 我从程序中获得以下代码示例:

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

using namespace std;

int main() {

    string ldapServerUrl = "192.168.10.29";
    int ldapServerPort = 389;

    LDAP* ldapSession = ldap_init(&ldapServerUrl[0], ldapServerPort);

    return 0;

}

When I try to build this sample with the mingw toolchain in Eclipse, the build fails and the line with ldap_init() is underlined in red. 当我尝试在Eclipse中使用mingw工具链构建此示例时,构建失败,并且带有ldap_init()的行用红色下划线标出。 When I hover the mouse over the error, it says "Undefined reference to _imp__ldap_initA() ." 当我将鼠标悬停在错误上时,它说“对_imp__ldap_initA()未定义引用”。 When I try it with the cygwin toolchain, it yields a similar error (with slightly different underscore arrangement). 当我使用cygwin工具链尝试时,会产生类似的错误(下划线排列略有不同)。

When I try to compile via cmd ( cd to directory then g++ main.cpp , cygwin is in PATH), I get this error: 当我尝试通过cmd编译(将cd转到目录,然后g++ main.cpp ,cygwin在PATH中)时,出现以下错误:

/cygdrive/c/Users/SomeUser/AppData/Local/Temp/ccZczWy3.o:main.cpp:(.text+0x68):
undefined reference to `__imp_ldap_init'
/cygdrive/c/Users/SomeUser/AppData/Local/Temp/ccZczWy3.o:main.cpp:(.text+0x68):
relocation truncated to fit: R_X86_64_PC32 against undefined symbol `__imp_ldap_init'
collect2: error: ld returned 1 exit status

I've read some about the __imp_ prefix being related to linking (this stuff is a bit over my head, normally I'm an Android developer). 我已经读过一些与链接有关的__imp_前缀(这个东西有点让我__imp_ ,通常我是Android开发人员)。 One term I came across was declspec . 我碰到的一个名词是declspec I took a look at winldap.h and it has the following (relevant?) code: 我看了winldap.h ,它具有以下(相关?)代码:

#ifndef WINLDAPAPI
#define WINLDAPAPI DECLSPEC_IMPORT
#endif

But from there, I have no idea where to go. 但是从那里,我不知道该去哪里。 How do I get this to compile? 我该如何进行编译?

It turns out that I did not have the Windows SDK installed. 原来,我没有安装Windows SDK The first thing I had to do was download and install it. 我要做的第一件事是下载并安装它。

Then, in Eclipse, I had to make some changes to the project configuration. 然后,在Eclipse中,我必须对项目配置进行一些更改。

First, I had to add the Windows SDK to the paths and symbols include directories (Right click project -> Properties -> C/C++ General -> Paths and Symbols -> GNU C++ -> Add...). 首先,我必须将Windows SDK添加到路径和符号包含目录(右键单击项目->属性-> C / C ++常规->路径和符号-> GNU C ++->添加...)。 In my case, the directory I had to add was C:\\Program Files (x86)\\Windows Kits\\8.1\\Lib\\winv6.3\\um\\x64 . 就我而言,我必须添加的目录是C:\\Program Files (x86)\\Windows Kits\\8.1\\Lib\\winv6.3\\um\\x64

Next, I had to add the Windows LDAP library to the Cygwin C++ Linker Library Configuration (Right-click project -> Properties -> C/C++ Build -> Settings -> Cygwin C++ Linker -> Libraries). 接下来,我必须将Windows LDAP库添加到Cygwin C ++链接程序库配置中(右键单击项目->属性-> C / C ++构建->设置-> Cygwin C ++链接程序->库)。 Under "Libraries (-l)," add Wldap32.Lib (case sensitive!) . 在“库(-l)” ,添加Wldap32.Lib (区分大小写!)

Now it builds and runs! 现在它可以构建并运行了!

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

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