简体   繁体   English

如何将 curl 链接到一个程序,以便我可以在另一台机器上运行它?

[英]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:为了解决这个问题,我尝试了以下操作:

  • linking libcurl.dll.a and libcurl.a with Linker settings in Bulid Options and Compiler both x64 and x86 versions,libcurl.dll.alibcurl.a与 Bulid Bulid OptionsCompiler中的Linker settings链接,包括 x64 和 x86 版本,
  • adding ..\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\includeBulid OptionsCompiler中的Search directories
  • writing -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:但是有多种解决方案:

Static build Static 构建

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文件。

Shared build共享构建

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.

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