动态库在运行时引发崩溃(Windows)

huangapple go评论61阅读模式
英文:

Dynamic library cause crash at runtime (Windows)

问题

I'm currently trying to create a dynamic library (just a test to see how it works) and so far I've been able to create a .dll file and link it to another .c file but when I'm running my program nothing is shown on the terminal (even if there is a printf() before a call to the library function)

Here are the files for the dll :

test.h

#pragma once
void foo();

test.c

#include <stdio.h>
#include "test.h"

void foo() {
printf("LOLOLOLO\n");
}

That I've compiled with :

gcc test.c -c -o test.o
gcc test.o -fPIC -shared -o libtest.dll

My main .c file which import my dll (that I won't rewrite it here because it does nothing except call foo in main with just a printf before and after foo to check that everything is doing fine) compile with no errors nor warnings because I added the -L... and -I... necessary (and -ltest). So I don't understand why it does not run.

Thanks in advance for the answers !

Edit : I just want to precise that if I export my library statically, it works perfectly

Edit2 : Here is the main program using foo

#include <stdio.h>
#include "test.h"

int main() {
printf("start\n");
foo();

printf("end\n");
return 0;

}

英文:

I'm currently trying to create a dynamic library (just a test to see how it works) and so far I've been able to create a .dll file and link it to another .c file but when I'm running my program nothing is shown on the terminal (even if there is a printf() before a call to the library function)

Here are the files for the dll :

test.h
-----------------
#pragma once
void foo();

test.c
-----------------
#include &lt;stdio.h&gt;
#include &quot;test.h&quot;

void foo() {
    printf(&quot;LOLOLOLO\n&quot;);
}

That I've compiled with :

gcc test.c -c -o test.o
gcc test.o -fPIC -shared -o libtest.dll

My main .c file which import my dll (that I won't rewrite it here because it does nothing except call foo in main with just a printf before and after foo to check that everything is doing fine) compile with no errors nor warnings because I added the -L... and -I... necessary (and -ltest).
So I don't understand why it does not run.

Thanks in advance for the answers !

Edit : I just want to precise that if I export my library statically, it works perfectly

Edit2 : Here is the main program using foo

#include &lt;stdio.h&gt;
#include &quot;test.h&quot;

int main() {
    printf(&quot;start\n&quot;);
    foo();

    printf(&quot;end\n&quot;);
    return 0;
}

答案1

得分: 1

我找到了解决方案。

我不知道在Windows上是必要的(因为所有教程都是在Linux上),要使用命令__declspec(dllexport)才能使库正常工作。

我希望它将来能帮助到某人!

英文:

I've found the solution.

I didn't know that it was NECESSARY on windows (because all the tutorials are on linux) to use the command __declspec(dllexport) for the library to works.

I hope it'll help someone in the future !

huangapple
  • 本文由 发表于 2023年6月6日 09:21:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76410879.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定