[英]How to link curl to a program so I can run it on another machine?
I'm trying to link curl to a program in CodeBlocks and then run it on another PC with Windows. If I set environmental variable %PATH% everything works fine, however if I remove it an error appears saying libcurl-x64.dll not found
.我正在尝试将 curl 链接到 CodeBlocks 中的一个程序,然后在另一台 PC 上使用 Windows 运行它。如果我设置环境变量 %PATH% 一切正常,但是如果我删除它,则会出现一条错误
libcurl-x64.dll not found
.
To fix that I tried following:为了解决这个问题,我尝试了以下操作:
libcurl.dll.a
and libcurl.a
with Linker settings
in Bulid Options
and Compiler
both x64 and x86 versions,libcurl.dll.a
和libcurl.a
与 Bulid Bulid Options
和Compiler
中的Linker settings
链接,包括 x64 和 x86 版本,..\curl-7.86.0_2-win/*(32/64)*/-mingw\include
to Search directories
in Bulid Options
and Compiler
..\curl-7.86.0_2-win/*(32/64)*/-mingw\include
到Bulid Options
和Compiler
中的Search directories
-lcurl
, -static-lcurl
, -DCURL_STATICLIB -lcurl
, -lws2_32
and -lwinmm
in every variations that would make sense.-lcurl
、 -static-lcurl
、 -DCURL_STATICLIB -lcurl
、 -lws2_32
和-lwinmm
。 However the same error still appears.但是仍然出现相同的错误。 How can l fix it?
我该如何解决?
CB Project file: CB项目文件:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="download" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/download" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
<Add directory="C:/libs/curl-7.86.0_2-win32-mingw/include" />
<Add directory="C:/libs/curl-7.86.0_2-win64-mingw/include" />
</Compiler>
<Linker>
<Add library="C:/libs/curl-7.86.0_2-win32-mingw/lib/libcurl.dll.a" />
<Add library="C:/libs/curl-7.86.0_2-win64-mingw/lib/libcurl.dll.a" />
<Add directory="C:/libs/curl-7.86.0_2-win32-mingw/lib" />
<Add directory="C:/libs/curl-7.86.0_2-win64-mingw/lib" />
</Linker>
</Target>
<Target title="Release">
<Option output="bin/Release/download" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
<Add option="-fexceptions" />
<Add directory="C:/libs/curl-7.86.0_2-win32-mingw/include" />
<Add directory="C:/libs/curl-7.86.0_2-win64-mingw/include" />
</Compiler>
<Linker>
<Add option="-lcurl -lwinmm -static" />
<Add library="C:/libs/curl-7.86.0_2-win64-mingw/lib/libcurl.a" />
<Add library="C:/libs/curl-7.86.0_2-win32-mingw/lib/libcurl.a" />
<Add library="C:/libs/curl-7.86.0_2-win32-mingw/lib/libcurl.dll.a" />
<Add library="C:/libs/curl-7.86.0_2-win64-mingw/lib/libcurl.dll.a" />
<Add directory="C:/libs/curl-7.86.0_2-win32-mingw/lib" />
<Add directory="C:/libs/curl-7.86.0_2-win64-mingw/lib" />
</Linker>
<Unit filename="main.cpp" />
<Extensions>
<lib_finder disable_auto="1" />
</Extensions>
</Project>
</CodeBlocks_project_file>
Some people call this "DLL hell", as it can be hard to figure out if you're new to this.有些人将此称为“DLL 地狱”,因为如果您是新手可能很难弄清楚。
But there are multiple solutions:但是有多种解决方案:
You could build a static executable using linker flag -static
.您可以使用 linker 标志
-static
构建一个 static 可执行文件。 However this also requires linking with all the dependencies of your dependencies, etc...然而,这也需要链接你的依赖项的所有依赖项,等等......
This can be hard to do as MinGW requires the -l
to be in the right order too.这可能很难做到,因为 MinGW 也要求
-l
的顺序正确。
Also, you may still have dependencies on the standard library, so you may also need to add -static-libgcc
(for C) or -static-libstdc++
(for C++).此外,您可能仍然依赖于标准库,因此您可能还需要添加
-static-libgcc
(对于 C)或-static-libstdc++
(对于 C++)。
If you get your dependency linker flags with pkg-config
you can use the pkg-config --static --libs
to list the static dependencies.如果您使用
pkg-config
获得依赖项 linker 标志,则可以使用pkg-config --static --libs
列出 static 依赖项。
Note that you can actually use pkg-config
in Code::Blocks linker flags if you surround it with back-quotes.请注意,如果用反引号括起来,您实际上可以在 Code::Blocks linker 标志中使用
pkg-config
。
The advantage of a static build is that you only need to distribute the .exe
file(s). static 版本的优点是您只需要分发
.exe
文件。
If you do a shared build (which is the case by default, but there is a -shared
linker flag) your .exe
file(s) will depend on the .dll
files of the program's dependencies, and these .dll
files will depend on the .dll
files of their dependencies, etc...如果您执行共享构建(默认情况下是这种情况,但有一个 -shared
-shared
标志)您的.exe
文件将依赖于程序依赖项的.dll
文件,而这些.dll
文件将依赖于它们的依赖项的.dll
文件等...
But if you copy all the .dll
files in the same folder as the .exe
file(s) you can distribute your application and it will run on other systems.但是,如果您将所有
.dll
文件复制到与.exe
文件相同的文件夹中,您就可以分发您的应用程序,它将在其他系统上运行。
Since the .dll
files are in the same folder as the .exe
file(s) Windows will pick them up there and there is no need to point to their location using the PATH
environment variable.由于
.dll
文件与.exe
文件位于同一文件夹中,因此 Windows 将在那里提取它们,无需使用PATH
环境变量指向它们的位置。
Copying all the right .dll
files can be a challenge though, so I have written a tool to detect which .dll
files .exe
files depend on and copies those files.复制所有正确的
.dll
文件可能是一个挑战,因此我编写了一个工具来检测.dll
文件.exe
文件依赖于哪些文件并复制这些文件。 The tool is available here: https://github.com/brechtsanders/pedeps该工具可在此处获得: https://github.com/brechtsanders/pedeps
To copy an .exe
file with all .dll
files it needs you need to run copypedeps
with the -r
flag (to recurse through each dependency).要复制包含所有
.dll
文件的.exe
文件,您需要使用-r
标志运行copypedeps
(以递归每个依赖项)。
For example you have a file hello.exe
in the current folder and you want to copy it along with all its dependencies to the folder dist
run the following command:例如,你在当前文件夹中有一个文件
hello.exe
,你想将它连同它的所有依赖项一起复制到文件夹dist
中,运行以下命令:
copypedeps -r hello.exe dist
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.