Solved

C/C++ with msvc 1.52

Posted on 2006-07-11
9
427 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
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

773 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