简体   繁体   中英

Windows C++ __imp reference error when building

Environment:

  • Windows Server 2012 R2
  • Eclipse Luna
  • cygwin
  • MinGW

I am building a C++ program that queries against Active Directory using LDAP (similar to this MSDN page ). 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. When I hover the mouse over the error, it says "Undefined reference to _imp__ldap_initA() ." When I try it with the cygwin toolchain, it yields a similar error (with slightly different underscore arrangement).

When I try to compile via cmd ( cd to directory then g++ main.cpp , cygwin is in PATH), I get this error:

/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). One term I came across was declspec . I took a look at winldap.h and it has the following (relevant?) code:

#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. 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.

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...). In my case, the directory I had to add was 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). Under "Libraries (-l)," add Wldap32.Lib (case sensitive!) .

Now it builds and runs!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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