Solved

C/C++ with msvc 1.52

Posted on 2006-07-11
9
435 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

809 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