Solved

C/C++ with msvc 1.52

Posted on 2006-07-11
9
407 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers 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.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now