Solved

C/C++ with msvc 1.52

Posted on 2006-07-11
9
438 Views
Last Modified: 2010-04-15

Hello,

I have a DLL application coded in C.

I want to include a C++ header file that I just created to 1 of the C file in this DLL application. Is it possible?

Thanks
0
Comment
Question by:TungVan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 43 total points
ID: 17083976
That depends on what is in the header file. If some is C++ specific, you might need to make it 'invisible' for the C compiler by using

#ifdef __cplusplus

// C++ specific stuff here

#endif
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 17084434
Hi TungVan,

The compiler (Ms C8) that shipped with MSVC 1.5.2 is not terribly flexible but it is fully capable of handling both C++ code and C code. Have you tried it? What problems are you having?

Paul
0
 
LVL 86

Expert Comment

by:jkr
ID: 17084578
>>but it is fully capable of handling both C++ code and C code

You will run into trouble if you use e.g. a 'class' in a file with a .c extension.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:PaulCaswell
ID: 17084661
jkr is right!

If you need to include a C++ header, try changing the extension of the .C file to .CPP.

What do you need to access from the header? If its just globals or #defines, consider moving those out to a separate header file. If you need to create/access the class you will need to make the compiler treat it as C++ by changing its extension.

Paul
0
 

Author Comment

by:TungVan
ID: 17119517

>Paul

I cannot change the extension (that's one of the requirement) and I won't create classes in this c file.

I just wonder if I put the C++ code in this C file, would the compiler able to compile this DLL?
0
 
LVL 14

Assisted Solution

by:cwwkie
cwwkie earned 41 total points
ID: 17121171
> I just wonder if I put the C++ code in this C file, would the compiler able to compile this DLL?

can you give an example of the c++ code you are trying to compile as c?


> I want to include a C++ header file that I just created to 1 of the C file in this DLL application. Is it possible?

put extern "C" around the declarations, for example:

extern "C" {

    void dllfunc1(void);
    int dllfunc2(int, char*);

}

extern "C" is c++ syntax, so to be able to compile that on both c as c++, you have to combine that with the first comment of jkr:

#ifdef __cplusplus
extern "C" {
#endif

    void dllfunc1(void);
    int dllfunc2(int, char*);

#ifdef __cplusplus
}
#endif
0
 
LVL 16

Assisted Solution

by:PaulCaswell
PaulCaswell earned 41 total points
ID: 17124882
You may be able to lie to the compiler by using #include "File.cpp" inside a "CFile.C" but that should be avoided if at all possible.

Paul
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question