1
votes

I've got a problem mixing the C++ and C code. My C++ code was created in VS10, i.e is a Windows Form project. I want include my C function in my C++ function through of linker (.obj). The steps are:

  • Windows Form project
    • VS10 default project
    • Call the C function
    • Build the project manually using msbuild
  • C code
    • Build the project manually through a make file using nmake and generates the objects files (.obj)

With the every objects files in hands (Cpp and C), the objects are linked in a third makefile. It's a simple idea but it doesn't work. The build in msbuild show the following message:

error LNK2028: unresolved token (0A00000C) "extern "C" void __clrcall MinhaFuncao(void)" (?MinhaFuncao@@$$J0YMXXZ) referenced in function "int __clrcall main(cli::array^)" (?main@@$$HYMHP$01AP$AAVString@System@@@Z)

error LNK2019: unresolved external symbol "extern "C" void __clrcall MinhaFuncao(void)" (?MinhaFuncao@@$$J0YMXXZ) referenced in function "int __clrcall main(cli::array^)" (?main@@$$HYMHP$01AP$AAVString@System@@@Z)

The Cpp code:

    #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;
}

The C code:

#include "complex.h"


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

The header file:

#ifndef COMPLEX_H_
#define COMPLEX_H_

#ifdef __cplusplus
extern "C"
{
#endif

#include <stdio.h>

void MinhaFuncao();



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

Has someone got an idea about this problem? I read others posts about linker issues, but the solutions proposed don't work for me. I believe that the difference is due to the msbuild and the VS project... :/

1
So, are you compiling your C code as C code? I.e. is your compiler working in C mode when compiling your C code? In any case, extern "C" { around #include "complex.h" is excessive: everything that needs to be done is already done inside complex.h.AnT
And this is not C++. This is C++-CLI apparently. Meanwhile, your question talks about C++. What is the task then: call C function from C++CLI or call C function from C++?AnT
notice the __clrcall in the error message, you need to make sure your function declarations aren't in a managed sectionAlan Birtles
@AnT are you compiling your C code as C code? I don't certain about that, I believe that my C code was compiled as C++. "C++-CLI ": I don't know CLI, I checked the wikipedia now about that, and it isn't my idea use it.Fernando
@Fernando: Um... Yes, I usually compile my C code as C code. Why?AnT

1 Answers

0
votes

You are using extern "C" in an extern C block

remove the outer extern C block surrounding #include "complex.h"