3つの関数を含む小さなライブラリをcで作成しようとしています。ここに私のコードです:dlfcn.hライブラリ関数を使用しているときにELFヘッダーエラーが無効です
mm_alloc.h:上記の3つの機能は、今
mm_test.c
#include "assert.h"
#include "dlfcn.h"
#include "stdio.h"
#include "stdlib.h"
/* Function pointers to hw3 functions */
void* (*mm_malloc)(size_t);
void* (*mm_realloc)(void*, size_t);
void (*mm_free)(void*);
void load_alloc_functions() {
void *handle = dlopen(".(Its path here)../mm_alloc.h", RTLD_NOW);
if (!handle) {
fprintf(stderr, "%s\n", dlerror());
exit(1);
}
char* error;
mm_malloc = dlsym(handle, "mm_malloc");
if ((error = dlerror()) != NULL) {
fprintf(stderr, "%s\n", dlerror());
exit(1);
}
mm_realloc = dlsym(handle, "mm_realloc");
if ((error = dlerror()) != NULL) {
fprintf(stderr, "%s\n", dlerror());
exit(1);
}
mm_free = dlsym(handle, "mm_free");
if ((error = dlerror()) != NULL) {
fprintf(stderr, "%s\n", dlerror());
exit(1);
}
}
int main() {
load_alloc_functions();
}
ための空での内部
/*
* mm_alloc.h
*
* A clone of the interface documented in "man 3 malloc".
*/
#pragma once
#include <stdlib.h>
void *mm_malloc(size_t size);
void *mm_realloc(void *ptr, size_t size);
void mm_free(void *ptr);
私のOSはUbuntuのです。コードをコンパイルする方法は次のとおりです。tmpを実行すると、「ELFヘッダーが無効です」というメッセージが表示されます。これをどうすれば解決できますか?
トピックはありませんが、 'mm_malloc'と友人は16バイトの位置合わせデータを扱います。交換用のアライメント品質が同じであることを確認してください。いくつかのシステムでは、16バイトの境界に合わせてヒープメモリを用意していないので、私は言います(Solarisは気になります)。 – jww
これを私の心に残しておきます。 – JollyRoger