混合C ++和C代码-VS10中的链接器问题

时间:2018-08-20 19:51:30

标签: c c++-cli

我在混合C ++和C代码时遇到问题。我的C ++代码是在VS10中创建的,即一个Windows Form项目。我想通过链接器(.obj)在C ++函数中包含C函数。步骤是:

  • Windows窗体项目
    • VS10默认项目
    • 调用C函数
    • 使用msbuild手动构建项目
  • C代码
    • 使用nmake通过make文件手动构建项目并生成目标文件(.obj)

每个对象文件都放在手中(Cpp和C),这些对象将链接到第三个makefile中。这是一个简单的想法,但不起作用。 msbuild中的构建显示以下消息:

  

错误LNK2028:未解析的令牌(0A00000C)“外部” C“ void __clrcall MinhaFuncao(void)”(?MinhaFuncao @@ $$ J0YMXXZ)在函数“ int __clrcall main(cli :: array ^)”中引用? @@ $$ HYMHP $ 01AP $ AAVString @ System @@@@ Z)

     

错误LNK2019:未解析的外部符号“ extern“ C” void __clrcall MinhaFuncao(void)”(?MinhaFuncao @@ $$ J0YMXXZ)在函数“ int __clrcall main(cli :: array ^)”中引用(?main @@ $$ HYMHP $ 01AP $ AAVString @ System @@@@ Z)

Cpp代码:

    #include "stdafx.h"
#include "Form1.h"
//#include"complex.h"
extern "C" {
#include "complex.h"
}

/*extern "C" {
void MinhaFuncao();
}*/
extern "C" void MinhaFuncao();

using namespace WFormTesting;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
    // Enabling Windows XP visual effects before any controls are created
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false); 

    MinhaFuncao(); //<--- Calling the Function HERE -->

    // Create the main window and run it
    Application::Run(gcnew Form1());
    return 0;
}

C代码:

#include "complex.h"


void MinhaFuncao()
{
    printf("My function AOWWW.\n");
}

头文件:

#ifndef COMPLEX_H_
#define COMPLEX_H_

#ifdef __cplusplus
extern "C"
{
#endif

#include <stdio.h>

void MinhaFuncao();



#ifdef __cplusplus
} // extern "C"
#endif
#endif

有人对这个问题有想法吗?我阅读了其他有关链接器问题的文章,但提出的解决方案对我不起作用。我相信差异是由于msbuild和VS项目...:/

1 个答案:

答案 0 :(得分:0)

您正在extern C块中使用extern“ C”

删除外部外部C块周围 #include "complex.h"

相关问题