Skip to content Accesskey=4Skip to sub-navigation Accesskey=3View our Accessibility Options MIT Information Systems Home About IS&T Contact IS&T Site Map Search Advanced Search
Getting StartedGetting Services by Topic or Alphabetically Getting Help

On This Page

[Help]

  

Quick Links

Top Level

Related Links

Ask OLC a question

Athena Consulting Homepage

Helpdesk Stock Answers (for Mac/PC questions)


Linking C++ with C

In order to link C procedures into C++ programs, you need to declare your C
functions in your C++ programs by declaring them in your C++ file.  An example
is

	extern "C" {
	        double bleft(double);
	}

which declares sqrt to be a function that takes a double as an argument and
returns a double.  This information is needed so that C++ can check that your
uses of this function have the correct type.

If you're using makefiles, you can have the files combined automatically.
You'd could use a Makefile like:

	SRCS = foo.C bar.c
	OBJS = foo.o bar.o


	.SUFFIXES: .C .o
	.C.o: ; $(C++) $(C++FLAGS) -c $<
	C++=g++


	prog: $(OBJS)
		$(C++) $(C++FLAGS) -o prog $(OBJS) -lm

to link together "foo.C" (the c++ file) and "bar.c" (ordinary C) to make the
executable program "prog".

MIT Home | Getting Started | Getting Services | Getting Help | About IS&T | Accessibility
Ask a technology question or send a comment about this web page.