是否可以将C代码包含在.NET C ++ / CLI应用程序中?

时间:2018-02-13 16:04:59

标签: c .net c++-cli

我试图将C代码库添加到我的Visual C ++ / CLI应用程序中(使用UI表单,并使用CLR编译)。 我正在使用块extern "C"{...},但由于下一个错误而无法编译:

error D8045: cannot compile C file 'Device.c' with the /clr option

如何包含此代码?谢谢。

(在SO中存在下一个帖子:D8045: cannot compile C file 'serialcommands.c' with the /clr option,但似乎没有明确解决......)

提前谢谢

编辑:我的代码是下一个:

// UI main .h class

extern "C"{
#include "Device.h"
}

EDIT2:正如我所说,这不是重复。甚至不是问题How can I insert C code into C++/CLI code?,这也是我的问题。在那个问题中,我询问了错误D8045并因延长我的问题而收到了不好的声誉。所以我打开了这个,以便询问错误。

1 个答案:

答案 0 :(得分:0)

您是否尝试右键单击c文件。 属性 - > C / C ++ - >常规:  “公共语言运行时支持”= 没有公共语言运行时支持

请注意,在c文件包含的头文件中,应该没有托管代码。通常,更容易保持大脑安全,只需制作一个新项目并保持清洁。

项目设置/ CLR /无预编译标题

两者都使用我的标题

select t.ts,
           max(item) over (partition by grp_item) as item,
           max(itemA) over (partition by grp_item) as itemA,
           max(itemB) over (partition by grp_item) as itemB
from (select t.*,
                    count(item) over (order by ts) as grp_item,
                    count(itemA) over (order by ts) as grp_itemA,
                    count(itemB) over (order by ts) as grp_itemB
         from t
       ) t;

我的c代码/没有CLR编译

#pragma once

#ifdef __cplusplus
extern "C" {
#endif

void func();

#ifdef __cplusplus
}
#endif

CheckCompileWithC.cpp:主项目文件。

#include "Souce.h"
#include <stdio.h>

void func()
{
    printf("test");
}

输出

#include "Souce.h"

using namespace System;

int main(array<System::String ^> ^args)
{
    Console::WriteLine(L"Hello World");
    func();
    return 0;
}