How to create a makefile for .c and .cpp

I am learning how to create makefiles using the VS2010 compiler on Win7.  I will need to call some c++ from c and I'm working on a trivial example to learn how to do this. I get different errs depending on whether I set CFLAGS=/TP or CFLAGS=/TC. Please tell me what flags I need to use to get c to work with c++.
Who is Participating?
thehagmanConnect With a Mentor Commented:
According to this, I think that neither /TP nor /TC should be necessary as you seem to name your files correctly and these flags influence which language (=syntax) is expected in the source files.

I guess your problem is rather at the linking state and has to do with function name mangling. However, I don't know how this works with cl.exe, but you may find something under that keyword ...
anAppBuilderAuthor Commented:
Thank you for the pointer thehagman.  I looked at these references:
Elegantly call C++ from C
How to call C++ function from C?

I have updated the header file and errors file.  With CFLAGS=/TC I now get an exe, but I still get this error:

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xlocale(323) : wa
rning C4530: C++ exception handler used, but unwind semantics are not enabled. S
pecify /EHsc
        cl /Fe"hellomake.exe" hellomake.obj hellofcn.obj hellofcn2.obj
jkrConnect With a Mentor Commented:
Well, it's just a warning, and the compiler already is telling you what to do:
Specify /EHsc

As a side note, there is absolutely no need to use either '/TC' or '/TP' in your case. That is only necessary hwne compiling files that don't have a .c, .cpp or.cxx extension.
Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

sarabandeConnect With a Mentor Commented:
in newhellomake.h you should add protection against including the same header twice. add

#pragma once

Open in new window

at top of the header file. if you want a portable header file (to Linux systems for example), use

// put here the contents of newhellomake.h
// with the __cplusplus switch
#endif  /* last statement in the header */

Open in new window

note, the name HELLOMAKE_H is optional. you should make sure that it wasn't used twice. so deriving it from filename is good praxis.

the /TP and /TC switch you were asking for, is not only unnecessary as already told by thehagman and jkr but overrules the defaults the compiler used for your source files. so it is clear why using the /TC works, cause it makes that both *.c and *.cpp file were compiled by c++ compiler. c is a subset of c++ (but not reverse). however I don't know why the printf and scanf calls would not generate errors when using /TC cause your .c files don't include stdio.h (and neither any other header which may provide the prototypes for printf and scanf.

the /TP would try to compile with ANSI-C compiler. here the scanf and printf correctly were refused because of the missing include. if you would include stdio.h the compiler would be able to compile the .c files but it would fail for the cpp files because of the stl template classes and usage of std namespace and std types.

anAppBuilderAuthor Commented:
Thank you jkr and thank you sarabande for several useful suggestions.

jkr, I still have one question.  I added  /EHsc:
but I still get the "Specify /EHsc" warning.

What am I missing?
sarabandeConnect With a Mentor Commented:
for the c compiler the /EHsc has no meaning cause c has no c++ exception handling.

for the c++ compiler the warning relates to a stl header xlocale. I would assume the warning is due to missing compiler options or to wrong environments.

did you run the vcvarsall.bat from command line prior to calling nmake?

for the options I would check the options set by visual studio for a win32 console project. you could create an empty test project in vs, switch of precompiled header in the c++ settings and check which command line options were used by visual c++ (see in c++ properties - commandline).

anAppBuilderAuthor Commented:
Thank you all.  The initial comment on name mangling provided the pointer to understand what was happening.  The later detail comments allowed me to complete the solution.

I was not able to eliminate the /EHsc warning while compiling the C sources as C.

When I added #include "stdio.h" to the C sources and added /TP to compile them as C++ the warnings went away.  Since my real C source compiles fine under C++, I have chosen that path.
I was not able to eliminate the /EHsc warning while compiling the C sources as C.
if you get the warning with the c sources  (in the first log file it was with the c++ source) you might check that the c sources don't include c++ headers.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.