473,503 Members | 11,018 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

compiling cpp in mostly c

131 New Member
I've got a large C project that I'm too far along in to abandon altogether, but I'd like to use some basic cpp functionality. Is it possible to mix these because compilation fails whenever I try to call a cpp function.

Expand|Select|Wrap|Line Numbers
  1. // main.h
  2.  
  3. #ifndef __MYHEADER__
  4. #define __MYHEADER__
  5.  
  6. // std c includes
  7.  
  8. // defines
  9.  
  10. // cpp prototypes
  11. void setVector(const int &, const int &, const unsigned char &);
  12. // compiler didn't like this, so changed to
  13. void setVector(const int, const int, const unsigned char);
  14.  
  15. #endif
Expand|Select|Wrap|Line Numbers
  1. // vec.cpp
  2. #include <vector>
  3. #include "main.h"
  4.  
  5. using std::vector;
  6. // declared global vector here
  7.  
  8. void setVector(const int row, const int col, const unsigned char ch)
  9. {
  10.    myvec[row][col] = ch;
  11. }
  12.  
the actual call comes from a c source file, but when I compile the project, I get "unresolved external symbol _setVector referenced in function _WndProc@16". I know what this error means, but how would I fix it? compile and link separately?

I'm using visual C++ express 2008.

TIA
Jan 7 '13 #1
5 1663
weaknessforcats
9,208 Recognized Expert Moderator Expert
The error means you have created your project as Windows application and you have no WndProc().

Delete your project and create a new one. Your C/C++ properties under the Advanced tab should have at least
/D "WIN32" for 32-bit addressing. Your vector will work fine as 32-bit.

That assumes you want a Windows program. If not, like you were wanting a console application with a main() then:

1) Create a new Win32 project
2) When the wizard appears do not click Finish
3) Instead click Application settings
4) Select empty project and console application
5) Now click finish and off you go.
Jan 8 '13 #2
divideby0
131 New Member
It's api - no mfc or resource files. I created a new cpp file and added it to the otherwise all c project; setVector was called from within the default wndproc wm_command message.

Expand|Select|Wrap|Line Numbers
  1. case WM_COMMAND : 
  2.  
  3.       switch(LOWORD(wParam)) {
  4.          // process control ids
  5.       }
  6.  
  7.       setVector(...);  
  8. ...
  9. }
The app compiles and runs with the reference to setVector commented out.

The project has main.h and three source files

main.c - winmain
proc.c - default proc
vect.cpp - the function definitions and declarations for the vector

Under each source file's properties, I selected compile as c for the c files and compile as cpp for the cpp.

under project properties, the c/c++ command line reads

/Od /D "_MBCS" /Gm /EHsc /RTC1 /MDd /Fo"Debug\\" /Fd"Debug\vc90.pdb" /W3 /nologo /c /ZI /TC /errorReport:prompt

other than disabling precompiled headers and setting the subsystem to windows, every option was left at its defaults.

When I created the project, I selected "general" from types and "empty project" from templates. Should I choose Win32 for both instead?
Jan 8 '13 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
Do you have a proc.h and a vect.h?

You will need in main.c:

Expand|Select|Wrap|Line Numbers
  1. #include <proc.h>
  2. #include <vect.h>
  3.  
These headers are to contain the function prototypes of the functions you call in main.c. Without those prototypes, your calls will be marked "unresolved external reference".

Remember, each .c must be able to compile on its own. That means main.c must contain the code for the function or the function prototype.

It is the job of the linker to match your function call with your code for the function.
Jan 8 '13 #4
Banfa
9,065 Recognized Expert Moderator Expert
You are missing the extern "C" declaration in you header file that causes the C++ compiler to use the C naming convention instead of the C++ naming convention which mangles the names to support function overloading.

Because of this where you include main.h into a cpp file setVector gets a C++ name and where you include it into a C file (main.c containing WndProc ???) it gets a C name and then the linker can't match them up so you get an unresolved external.

try

Expand|Select|Wrap|Line Numbers
  1. // main.h
  2.  
  3. #ifndef __MYHEADER__
  4. #define __MYHEADER__
  5.  
  6. // std c includes
  7.  
  8. // defines
  9.  
  10. // function prototypes
  11. #if __cplusplus
  12. extern "C" {
  13. #endif
  14.  
  15. void setVector(const int, const int, const unsigned char);
  16.  
  17. #if __cplusplus
  18. }
  19. #endif
  20.  
  21. #endif
  22.  
This declares setVector as having C calling convention even in cpp files and should allow the function in vec.cpp to compile and be called from C language files.
Jan 8 '13 #5
divideby0
131 New Member
Thank you both; weakness, you've always been a huge help and banfa, I had no clue about extern "C" - your explanation was most helpful and did the trick.

I'm always learning something new here.
Jan 8 '13 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
1422
by: Michael Gaab | last post by:
on large commercial/scientific programs, which takes that longest, generating object code or linking? thanks, mike
14
1209
by: MJL | last post by:
I am working on a set of functions that involve dynamic memory allocation. I started working with gcc and then moved to Pacific C. The reason was that my only access to gcc was through an ssh...
2
1260
by: gurpreet | last post by:
Hi this is gurpreet, I know this is a very simple question but still I want to clear some doubts. What happens when we compile and link a c-program? I hope aquite a lot of responses to my...
3
980
by: Andreas Morgenstern | last post by:
Hi, Does anybody know how one can use a macro (or something similar) as a function name ? I would like to declare the following functions: FOO_fun1(int) { .... }
13
1383
by: Daniel Billingsley | last post by:
I have a solution consisting of 9 projects with various references to each other (clean though, no loops, etc.). Visual Studio sometimes gives me weird errors that it can't find one of the...
0
2260
by: follower | last post by:
This post is mostly Google-bait for anyone else that might want to compile SpiderMonkey ( libjs / libjs.so / libjs.dylib ) for OS X (10.4.5 in my case) and then use it with Python's ctypes. I can't...
7
1347
by: maya | last post by:
hi, I'm following example 'Intro7.aspx' here, http://www.csharpfriends.com/quickstart/aspplus/doc/webformsintro.aspx#customctrls it says on top of Acme.cs "namespace Acme" I assume this means...
2
2005
by: Jan Althaus | last post by:
Mostly for testing reasons I'd like to see if it makes sense to chose the following approach for just-in-time compilation of shaders for a renderer: Seeing as the shaders themsefs consist mostly...
14
2453
by: spamtrap | last post by:
Mostly for testing reasons I'd like to see if it makes sense to chose the following approach for just-in-time compilation of shaders for a renderer: Seeing as the shaders themsefs consist mostly...
3
1578
by: Mitko Haralanov | last post by:
For various reason, what I need to do is be able to send some Python code (mostly entire functions in the form of a string) to a remote server (written in Python), have that server compile the code...
0
7095
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7294
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4693
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3183
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1523
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
749
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
403
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.