Access Keys:
Skip to content (Access Key - 0)

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 sqrt(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".

IS&T Contributions

Documentation and information provided by IS&T staff members


Last Modified:

February 11, 2009

Get Help

Request help
from the Help Desk
Report a security incident
to the Security Team
Labels:
olc-c olc-c Delete
c c Delete
c++ c++ Delete
link link Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
Feedback
This product/service is:
Easy to use
Average
Difficult to use

This article is:
Helpful
Inaccurate
Obsolete
Adaptavist Theme Builder (4.2.3) Powered by Atlassian Confluence 3.5.13, the Enterprise Wiki