英文:
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 <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;
}
答案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 !
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论