【问题标题】:Error while compiling and linking dll编译和链接dll时出错
【发布时间】:2014-10-10 09:29:33
【问题描述】:

我正在尝试按照cygwin tutorial 编译一个简单的dll。除了最后一步,我已经能够成功完成所有工作。当我执行命令时:

gcc -o myprog myprog.c -L./ -lmydll

我收到一条错误消息,指出 hello() 未在该范围内声明。我按照教程逐字进行,但我仍然无法编译这个简单的项目,并且不知道为什么。

各个文件的代码如下:

(myprog.c)

int main(void){
    hello();
}

(mydll.c)

#include <stdio.h>
int hello(){
    printf("Hello World!\n");
    return 0;
}

【问题讨论】:

  • 您当然应该向我们展示 myprog.c 中的代码吗?
  • @JohnZwinck 已更新,但我认为这与网页没有区别。

标签: c gcc dll compilation


【解决方案1】:

这个例子在我的 Cygwin 中运行良好

armathew@3NJ2VQ1 /cygdrive/d/userdata/armathew/Desktop/WWWW
$ ls
mydll.c  myprog.c

armathew@3NJ2VQ1 /cygdrive/d/userdata/armathew/Desktop/WWWW
$ gcc -c mydll.c

armathew@3NJ2VQ1 /cygdrive/d/userdata/armathew/Desktop/WWWW
$ gcc -shared -o mydll.dll mydll.o

armathew@3NJ2VQ1 /cygdrive/d/userdata/armathew/Desktop/WWWW
$ gcc -o myprog myprog.c -L./ -lmydll

armathew@3NJ2VQ1 /cygdrive/d/userdata/armathew/Desktop/WWWW
$ ./myprog.exe
Hello World!

您使用的 Cygwin 版本是什么?我的是 1.7.5

armathew@3NJ2VQ1 /cygdrive/d/userdata/armathew/Desktop/WWWW
$ uname -r
1.7.5(0.225/5/3)

【讨论】:

    【解决方案2】:

    好吧,链接库的声明可能不正确。 应该是

    -L&lt;library path&gt; -lyoulibrarymane

    因为库路径后没有“./”。 这是我使用的一个示例,它可能会有所帮助。 -I/usr/local/include是头文件路径

    gcc -o hello-world helloopencv.c -I/usr/local/include -L/usr/local/lib -lopencv_highgui -lopencv_core -lopencv_imgproc

    【讨论】:

    • 如果.dll 与其他文件位于同一目录中,-L 会是什么。 .c.dll只有一个文件夹。
    • 只需删除推荐中的“./”,如果它不起作用,则尝试整个库路径。你可以通过推荐'pwd'来查看你当前的工作路径
    【解决方案3】:

    你需要在myprog.c的顶部添加一个声明:

    int hello(void);
    

    或者你可以把它放在一个新的 mydll.h 和 #include 到 myprog.c。

    【讨论】:

    • 为什么不包含在示例页面中?它只是暗示你会这样做吗?
    • 如果您在禁用警告的情况下编译,程序将“工作”。但是没有人应该在 2014 年甚至 2004 年编写这样的代码。教程代码在现代写得不好……就像过去 2.5 年一样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-20
    • 2012-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多