2016-01-13 44 views
6

所以我試圖做一個非常簡單的抽象庫,使用C的Windows API的頂部。我做了一個簡單的Makefile,當我嘗試使用Msys項目,我得到這個:奇怪的「未定義的引用」錯誤

winapi.o: In function `get_client_pid': 
(long path)/winapi.c:13: undefined reference to `GetExtendedTcpTable' 
(long path)/winapi.c:31: undefined reference to `GetExtendedTcpTable' 
collect2.exe: error: ld returned 1 exit status 
make: *** [lib] Error 1 

和get_client_pid()在winapi.c定義爲

#include <stdint.h> 
#include <stdio.h> 
#include <windows.h> 
#include <Iphlpapi.h> 

#define true 1 
#define false 0 


    uint32_t get_client_pid() 
    { 
     MIB_TCPTABLE_OWNER_PID *tcpTable; 
     PDWORD tableSize; 
     DWORD ret = GetExtendedTcpTable(tcpTable, 
             tableSize, 
             true, 
             2, 
             TCP_TABLE_OWNER_PID_ALL, 
             0); 

     if (ret == NO_ERROR) { 
      printf("ERROR get_client_pid: No error with table size at 0?\n"); 
      return 0; 
     } 
     if (ret != ERROR_INSUFFICIENT_BUFFER) { 
      printf("ERROR get_client_pid: No insufficient buffer with table size at 0?\n"); 
      return 0; 
     } 
     printf("TABLE SIZE %d\n", *tableSize); 
     tcpTable = malloc(*tableSize); 

     ret = GetExtendedTcpTable( tcpTable, 
            tableSize, 
            true, 
            2, 
            TCP_TABLE_OWNER_PID_ALL, 
            0); 

     if (ret != NO_ERROR) { 
      printf("ERROR get_client_pid: GetExtendedTcpTable error: %d\n", ret); 
      free(tcpTable); 
      return 0; 
     } 

     int i; 
     for (i = 0; i < tcpTable->dwNumEntries; i++) { 
      MIB_TCPROW_OWNER_PID row = tcpTable->table[i]; 
      if (row.dwRemotePort == 0x208 && row.dwRemoteAddr == 0x100007F) { 
       printf("FOUND PROCESS: %d\n", row.dwOwningPid); 
       free(tcpTable); 
       return row.dwOwningPid; 
      } 
     } 

     free(tcpTable); 
     return 0; 
    } 

由於這是一個鏈接錯誤,我要添加的makefile:

current_dir = $(shell pwd) 

all: test 

test: lib 
    gcc -c -g tests.c 
    gcc -o tests.exe tests.o -L$(current_dir) -lwinapi 

lib: 
    gcc -c -g winapi.c 
    gcc -shared -o winapi.dll winapi.o -lIphlpapi -luser32 

clean: 
    rm *.o 

我不得不說,我試圖在每個可能的地方插入「-IIphlpapi」,以防連接以某種方式錯誤順序,但這並沒有幫助。

+0

C++名稱重整問題? –

+0

嘗試添加一個來自user32的函數調用,看看這個鏈接 –

+0

我不完全滿意你的makefile btw。 winapi.dll應該依賴於winapi.o,它應該依賴於winapi.c。 'clean'應該刪除tests.exe和winapi.dll。 –

回答

2

MinGW在windef.h中將WINVER定義爲0x400(Windows NT),這意味着Windows的某些更新功能將不會鏈接。

#ifndef WINVER 
#define WINVER 0x0400 
/* 
* If you need Win32 API features newer the Win95 and WinNT then you must 
* define WINVER before including windows.h or any other method of including 
* the windef.h header. 
*/ 
#endif 

所以你需要之前自己定義WINVER到你的目標的Windows版本包括所有Windows頭。下面定義的

用途之一:

#define WINVER 0x0500 // Windows 2000 
#define WINVER 0x0501 // Windows XP 
#define WINVER 0x0502 // Windows Server 2003 
#define WINVER 0x0600 // Windows Vista, Windows Server 2008 
#define WINVER 0x0601 // Windows 7 
#define WINVER 0x0602 // Windows 8 
#define WINVER 0x0603 // Windows 8.1 
#define WINVER 0x0A00 // Windows 10