我試着動態加載與C和我已經遇到了問題,在非常設置。我有一個運行時加載對象的小程序。該對象包含一個將某些消息寫入stdin的函數。這是在OS X 10.10上使用clang編譯的。下面是代碼:C動態加載文件太短?
/* loader.c */
#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>
#include "module.h"
int main(int argc, char **argv) {
char file[] = "https://stackoverflow.com/users/user/dev/module.o";
void *handle;
int (*function)();
handle = dlopen(file, RTLD_NOW);
if (!handle) {
printf("Cannot load program. Error: %s\n", dlerror());
return 1;
}
program = dlsym(handle, "function");
printf("Program loaded");
function();
printf("Exiting");
return 0;
}
這裏module.h中是:
/* module.h */
int
function();
這裏的module.c:
/* module.c */
#include <stdio.h>
#include <unistd.h>
int function() {
printf("Hello from module");
sleep(1);
printf("Hello from module again");
return 0;
}
這裏是Makefile文件:
loader : loader.c module.o
cc -Wall loader.c -ldl -o loader
module.o : module.c
cc -Wall -fpic -c module.c
這編譯沒有警告,但它不執行的方式 我預計。該程序返回以下錯誤:
Error: dlopen(/users/user/dev/module.o, 2): no suitable image found. Did find: /users/user/dev/module.o: file too short
。
我已經看了一下,這裏的錯誤信息並不多。該計劃基於TLDP的dlopen
示例。這是否意味着文件需要具有一定的大小才能被動態加載,或者這些文件的編譯方式有問題嗎?
我覺得我錯過了一些簡單的東西。
如何讓該程序按預期執行?