c - 奇数 "undefined reference"错误

标签 c makefile linker-errors

因此,我尝试使用 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

我不得不说,我尝试了所有可能的地方来插入“-lIphlpapi”,以防链接顺序错误,但这没有帮助。

最佳答案

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 header 。

使用以下定义之一:

#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

关于c - 奇数 "undefined reference"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34776543/

相关文章:

ios - 为 iOS 构建后 Ionic 应用程序链接器错误

当行 > 1 时,C dup2 会覆盖文件错误

c - putchar() 奇怪的输出,为什么会这样?

c - 如何减少自己的 C 程序的 CPU 使用率

c++ - Makefile 报告 `func[abi:cxx11]()' 的多个定义

c++ - 破译损坏的 C++ 名称 $$F

linker-errors - IAR 错误 [Li005] : no definition for function even though the file is added in the project and the path to the file is in the pre-processor paths

c - 为什么我必须在等待特定信号之前设置 pthread_sigmask

c++ - 将几个静态文件组合成一个新文件

c++ - 使用 MinGW 在 Windows 上编译 PDCurses