return value from extern "C" not right

I am using Red Hat Linux.
I have two directories:
~/GetOneHundred
~/GetOneHundred/ReturnOneHundred

In ~/GetOneHundred/ReturnOneHundred, I have three files:
ReturnOneHundred.c
ReturnOneHundred.h
makefile

This is ReturnOneHundred.c:
1
2
3
4
5
6
#include "ReturnOneHundred.h"

float ReturnOneHundred() {
	float f = 100;
	return f;
}

This is ReturnOneHundred.h:
1
2
3
#include <stdio.h>

extern "C" float ReturnOneHundred();

This is my makefile:
1
2
3
4
5
6
7
8
All:  net/Net


net/Net:	 ReturnOneHundred.c
	g++ -g ReturnOneHundred.c -c

clean:
	rm ReturnOneHundred.o


In my ~/GetOneHundred directory, I have two files:
GetOneHundred.c
makefile

This is GetOneHundred.c:
1
2
3
4
5
6
7
8
#include <stdio.h>

int main( int argc, char **argv )
{
	float one = ReturnOneHundred();
	printf("return: %f\n", one);
	return 1;
}

and this is the makefile:
1
2
3
4
5
6
7
8
9
All:  Get/GetOneHundred

Get/GetOneHundred:	 GetOneHundred.c 
	cc -g GetOneHundred.c ReturnOneHundred/ReturnOneHundred.o \
        -lstdc++ -o GetOneHundred


clean:
	rm GetOneHundred


I "make" the files in the ~/GetOneHundred/ReturnOneHundred directory first, then I "make" the files in the ~/GetOneHundred directory. This all works.

However, when I execute the GetOneHundred executable, "100" is not printed to the screen. I get garbage values like "1120403456.000000" printed to the screen. When I go through with the debugger, float f is equal to 100 before I exit the function. Once I return from the function, float one is not equal to 100 anymore.

In ReturnOneHundred.c and .h, if I change everything to an int, I get "100" printed to the screen. What is going wrong?
The code shouldn't compile. extern "C" is not valid C.

extern "C" is a C++ construct.
Last edited on
So that the header file may be included by both C and C++, I often do this:
1
2
3
4
5
6
7
8
9
#ifdef __cplusplus
extern "C" {
#endif

//...

#ifdef __cplusplus
}
#endif 
Topic archived. No new replies allowed.