簡體   English   中英

在編譯時檢查靜態函數是否存在

[英]Checking if a static function exists at compile time

功能。 c (我不能編輯這個文件)

#define FOO    1

#if FOO == 1
    void Foo() {}
#endif

cp

class MyClass
{
#if FOO == 1
    void CppFoo()
    {
        Foo();
    }
#endif
}

我想做同樣的事情,但不使用 main.cpp 文件中的定義TEST

我想做的事:

class MyClass
{
#if (extern "c" Foo() exist)
    void CppFoo()
    {
        Foo();
    }
#endif
}

如果尚未聲明靜態函數Foo()則不必聲明CppFoo()方法。

我怎樣才能做到這一點?

您可以使用弱屬性,例如:

文件 ac:

#include <stdio.h>

int foo() __attribute__ ((weak));
int foo() {}

int main(int argc, char** argv)
{
    foo();
}

文件公元前:

#include <stdio.h>

int foo () {
    printf("Hello Wurld!\n");
}

現在,如果您不編譯和鏈接 bc,則調用默認(無操作)函數foo() ,否則調用 bc 中的函數foo()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM