简体   繁体   English

链接和编译后C执行错误

[英]C execution error after linking & compilation

I'm getting some strange results trying to link to an external library. 尝试链接到外部库时出现一些奇怪的结果。 If I run the following from the command line: 如果我从命令行运行以下命令:

gcc fftwTest.c -I../extlib/fftw-3.3.5-dll32 -L../extlib/fftw-3.3.5-dll32 -lfftw3-3 -lm -o test

test.exe compiles, but when I run it I get an error message saying the program can't start because libfftw3-3.dll is missing from my computer. test.exe可以编译,但是当我运行它时,我收到一条错误消息,提示该程序无法启动,因为我的计算机缺少libfftw3-3.dll。 Despite the fact that the linker found it when compiling. 尽管链接器在编译时找到了它。

If, instead, I place libfftw3-3.dll into the same directory as my fftwTest.c file and do -L to there, it compiles and runs OK. 相反,如果我将libfftw3-3.dll放在与fftwTest.c文件相同的目录中,并在其中执行-L,则它将编译并运行正常。 So I guess this suggests I'm doing something wrong with my -L command, but as it linked and compiled I don't know quite what. 所以我想这表明我的-L命令做错了什么,但是由于它链接并编译了,所以我不太清楚。

Any help would be appreciated. 任何帮助,将不胜感激。

Thanks! 谢谢!

Nothing is wrong with your link command, and yes, the linker found it while compiling because you specified the path to it, but that's a different story when running the program (the linker doesn't /cannot hardcode the path for the DLL, it could be located somewhere else on the deployment machine(s)). link命令没什么问题,是的,链接器在编译时找到了它,因为您指定了它的路径,但是在运行程序时情况就不同了(链接器不会/无法对DLL的路径进行硬编码,可以位于部署计算机上的其他位置)。

The DLL must be in the system path or in the same directory as the executable. DLL必须与可执行文件位于系统路径或同一目录中。 In your case you could: 在您的情况下,您可以:

  • copy the DLLs in the .exe directory (you found that yourself) 复制.exe目录中的DLL(您发现自己)
  • or add: set PATH=%PATH%;../extlib before starting your executable in a batch file (or set the full absolute path in the system path using Windows environment variable management) 或添加:在批处理文件中启动可执行文件之前, set PATH=%PATH%;../extlib (或使用Windows环境变量管理在系统路径中设置完整的绝对路径)

Copying the DLLs in the executable directory is better when you distribute the package to a third party so no configuration is needed, however, if you use/upgrade the DLL many times and on a lot of small programs, the best solution on your development machine is to keep it in one place and set the system PATH to run it (that's the tradeoff) 将软件包分发给第三方时,将DLL复制到可执行文件目录中会更好,因此不需要进行配置,但是,如果您多次使用DLL或在许多小程序上升级DLL,则这是开发计算机上的最佳解决方案是将其放置在一个位置并设置系统PATH来运行它(这是一个折衷方案)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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