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++.
makefile.txt
hellomake.c
hellomake.h
hellofcn.c
hellofcn2.cpp
Make-Errors.txt
anAppBuilderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

thehagmanCommented:
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 ...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
NEWhellomake.h
NewError.txt
0
jkrCommented:
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.
0
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

sarabandeCommented:
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

#ifndef HELLOMAKE_H
#define HELLOMAKE_H
// 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.

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

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

What am I missing?
NEW2hellomake.txt
0
sarabandeCommented:
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).

Sara
0
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.
0
sarabandeCommented:
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.

Sara
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.