为什么我的C程序可以运行在"git bash",但不能在"cmd"?

Why can my C program run in "git bash", but not in "cmd"?

我写了一个使用 libpq 连接到 PostgreSQL 数据库的演示。

我试图通过包含

将 C 文件连接到 PostgreSQL
#include <libpq-fe.h>

在我将路径添加到系统变量 I:\Program Files\PostgreSQL\lib 以及 I:\Program Files\PostgreSQL\include 并使用此命令编译之后:

gcc -Wall -Wextra -m64 -I "I:\Program Files\PostgreSQL\include" -L "I:\Program Files\PostgreSQL\lib" testpsql.c -lpq -o testpsql

首先报了三个错误,比如

libssl-1_1-x64.dll is missing
libintl-8.dll was missing
libcrypto-1_1-x64.dll was missing

我把这三个文件下载到I:\Program Files\PostgreSQL\lib后,再次编译,显示错误

The application was unable to start correctly (0xc0150002)

当我键入 testpsql 时。但是,如果我在 git bash 上键入 ./testpsql,它就会起作用。谁能告诉我为什么?

我使用的代码是here的第一个例子。

环境:PostgreSQL 12,Windows10,MinGW64

“下载 DLL 文件”听起来很危险。来自哪里?

我会再次删除这些文件。由于您可能没有从您的代码中引用这些库,因此它必须是 libpq.dll 的依赖项并且可能在 I:\Program Files\PostgreSQL\bin 中找到(如果您使用了 EDB 安装程序)。

问题可能是你的 PATH 环境变量在 git bashcmd.exe 中不同,在后一种情况下,并不是所有需要的共享库都可以在PATH。解决方案是更改 PATH 以便它包含可执行文件所需的所有 DLL 文件,而不是 开始复制文件。

PATH 中包含 I:\Program Files\PostgreSQL\bin 可能就足够了。要解决缺少的依赖项,请使用 dependency walker or this replacement.

之类的工具