Author Topic: mixing c++/c problem (yes it's something bad)  (Read 3476 times)

0 Members and 1 Guest are viewing this topic.

Offline hellsangel

  • C= 64
  • **
  • Posts: 46
  • Karma: 10
    • View Profile
yup

I see on the forum a post about the mixing of c/c++, but when it's main.c + functions.cpp : no problems.

But my problem is that the main is a c++ class, and the rest of program is C
strange thing happen :
when I made the project, after some fixes because of "stdafx.h" - the cpp use it as precompilated header, I desactived precomp header of each c files - at the first compilation I had the problem of lib conflict (libcmt, mscvrt) : "nafxcw.lib(afxmem.obj) : error LNK2005 operator new already defined in LIBCMT.lib" because some new/delete function was used, but not needed : I deleted them.
And the compilation was Ok : the program works fine.
And now the strange bug : If I close the project (vc6) and open it again, the error LNK2005 is back, but I've no more new/delete functions.
the first time it was a big chance, no ? now it doesnt work anymore if I dont remade all the project from scratch.
Is it possible to fix that ?
or to adapt the main class in C ?

thanks

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: mixing c++/c problem (yes it's something bad)
« Reply #1 on: January 06, 2008 »
That linker error is because you haven't used the same library settings for all the .libs/.obj files in your project.
The choices are 'dll', 'static', 'multithreaded dll', 'multithreaded static'.  Also there are debug versions of these.  Each project must use the same settings or not link to any runtime at all (/nodefaultlib in the linker options).  If you choose dll then all of them may be linked against the dll runtime.  If you choose static, then all modules should use /nodefaultlib except the main program.
I don't really know what you mean when you say that main is a c++ class.

Jim
Challenge Trophies Won:

Offline hellsangel

  • C= 64
  • **
  • Posts: 46
  • Karma: 10
    • View Profile
Re: mixing c++/c problem (yes it's something bad)
« Reply #2 on: January 06, 2008 »
thanks for you answer

the whole project is a plug in compiled as DLL in multithreaded

err, yes sorry for the mistake about "the class"
here is the source:

main.cpp
Code: [Select]
#include "stdafx.h"
#include "main.h"
#include <math.h>
#include "MainData.h" ; -> here is the main Class !

// create the MainData object using VTable : all exported functions of the plugin
extern "C" __declspec(dllexport) CMainData* CreateObject()
{
return new CMainData();
}

// specific plugin functions
extern "C" __declspec(dllexport) char* GetShortInfo()
{
return "name of plugin";
}

[...etc...]

// CMain construction

CMain::CMainApp()
{

}

// The one and only CMainApp object

CMainApp theApp;

main.h
Code: [Select]
class CMainApp : public CWinApp
{
public:
 CMainApp();
 DECLARE_MESSAGE_MAP()
};

maindata.cpp
Code: [Select]
#include "stdafx.h"
#include "MainData.h"

[...implementation of CMainData class...]

maindata.h
Code: [Select]
class CMainData
{
public:
 CMainData();           // protected constructor used by dynamic creation

//******** VTABLE *************** here the exported plugin functions
public:
 virtual int function1();
 ...
};

all cpp files have precomp headers set on StdAfx.cpp (StdAfx.h)
in this plugin project, I added my C program who have also exported functions

I verified the files, and all are "multithreaded"

difficult to explain. You can find the SDK and example I want to use (ColorWash) here

I can perhaps send you by pm the files from the project if you want
« Last Edit: January 06, 2008 by [Y] »

Offline hellsangel

  • C= 64
  • **
  • Posts: 46
  • Karma: 10
    • View Profile
Re: mixing c++/c problem (yes it's something bad)
« Reply #3 on: January 06, 2008 »
I tried again, and It seems to be a VC6 bug. If I open the project without the .C files and add and fix them during the work session : all is ok
But no more after I close the project.
I'll try with another IDE (difficult because I dont know enough devcpp or others ide)
marvellous bug  :xmas: