我需要爲客戶端提供一個C靜態庫,並且需要能夠使結構定義不可用。最重要的是,我需要能夠使用全局變量在庫初始化之前執行代碼。隱藏靜態庫中的結構定義
這裏是我的代碼:
private.h
#ifndef PRIVATE_H
#define PRIVATE_H
typedef struct TEST test;
#endif
private.c (this should end up in a static library)
#include "private.h"
#include <stdio.h>
struct TEST
{
TEST()
{
printf("Execute before main and have to be unavailable to the user.\n");
}
int a; // Can be modified by the user
int b; // Can be modified by the user
int c; // Can be modified by the user
} TEST;
main.c
test t;
int main(void)
{
t.a = 0;
t.b = 0;
t.c = 0;
return 0;
}
顯然,這代碼不工作...但顯示什麼,我需要做的...任何人知道如何使這項工作?我谷歌相當多,但無法找到答案,任何幫助將不勝感激。
TIA!
使用不完全類型的手柄允許編譯器捕捉類型的錯誤,並在實施功能避免鑄造。 – 2010-05-24 07:41:20
編譯器可能會_still_ catch類型錯誤,因爲您的庫代碼與正確類型的變量一起工作。或者,如果你確實意味着給你的函數賦予了錯誤的類型,那麼這是一個用戶錯誤,他們應該會帶來所有的困難:-)正如我所說,這是唯一的方法,而不是唯一的方法。 – paxdiablo 2010-05-24 07:44:15
爲什麼地球上會使用void *並丟失所有類型的檢查? typedef struct MyHiddenStruct MyHandle; MyHandle * allocHandle(); – 2010-05-24 07:54:26