By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,889 Members | 1,297 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,889 IT Pros & Developers. It's quick & easy.

extern "C" Problem (currently runs well without using them and CC,)

P: n/a
Hello,

THE PROBLEM
-----------
If I compile parts of my program with CC, and C, using extern "C" as
appropriate it compiles without any errors, but it does a segmentation
fault when I run the program.

The variables involved in the segmentation fault are surrounded by
compiler directives. I am 100% sure that I do not have to extern "C"
them.

UNRELATED EXAMPLE
=================
An example of using extern "C" is below to only show it. This is not
the program that I am referring to above. This example is for
unrelated background.

main.cpp
--------

extern "C" void test (int my_guess);

int main ()
{
int my_guess = 12580;

test (my_guess);
}

test.c
------
void test (int my_guess)
{
printf ("my_guess is %d\n",my_guess);
}

Compilation
-----------
cc -c test.c
CC main.c test.o

WHAT CAN I DO
-------------
What do you think the problem is? If I take the same program and
compile it just using the c compiler(using cc) without the extern
"C"'s the program compiles and runs perfectly! Could it be a
pre-compiler directive causing this? In both instance, where it would
run or not run I used the exact same makefile. The only change I made
in the makefile was to change CC to cc or vice versa. Should I just
step through my code with dbx to find the problem?

WHY I AM COMPILING THIS WAY
---------------------------
The reason that I want to be able to compile parts of the program with
CC and cc is I "have to" compile later with a C++ library which has a
lot of statements not in the C language.

Thank you,
Christopher Lusardi
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Christopher M. Lusardi wrote:


THE PROBLEM
-----------
If I compile parts of my program with CC, and C,
using extern "C" as appropriate it compiles without any errors,
but it does a segmentation fault when I run the program.

The variables involved in the segmentation fault
are surrounded by compiler directives.
I am 100% sure that I do not have to extern "C" them.

UNRELATED EXAMPLE
=================
An example of using extern "C" is below to only show it.
This is not the program that I am referring to above.
This example is for unrelated background.

main.cpp
--------

extern "C" void test(int my_guess);

int main() {
int my_guess = 12580;

test(my_guess);
}

test.c
------

void test(int my_guess) {
printf("my_guess is %d\n", my_guess);
}

Compilation
-----------
cc -c test.c
CC main.c test.o

WHAT CAN I DO
-------------
What do you think the problem is? If I take the same program and
compile it just using the c compiler(using cc) without the extern
"C"'s the program compiles and runs perfectly! Could it be a
pre-compiler directive causing this? In both instance, where it would
run or not run I used the exact same makefile. The only change
that I made in the makefile was to change CC to cc or vice versa.
Should I just step through my code with dbx to find the problem?

WHY I AM COMPILING THIS WAY
---------------------------
The reason that I want to be able to compile parts of the program with
CC and cc is I "have to" compile later with a C++ library which has a
lot of statements not in the C language. cat test.c #include <stdio.h>

void test(int my_guess) {
printf("my_guess is %d\n", my_guess);
}
cat main.cpp extern "C" void test(int my_guess);

int main(int argc, char* argv[]) {
int my_guess = 12580;
test(my_guess);
return 0;
}
gcc -Wall -std=c99 -pedantic -O2 -c test.c
gcc --version gcc (GCC) 3.2 20020903 (Red Hat Linux 8.0 3.2-7) g++ -Wall -ansi -pedantic -O2 -o main main.cpp test.o
g++ --version g++ (GCC) 3.2 20020903 (Red Hat Linux 8.0 3.2-7) ./main

my_guess is 12580
It works just fine for me.
Jul 22 '05 #2

P: n/a
Ian
Christopher M. Lusardi wrote:

<snip>
WHAT CAN I DO
-------------
What do you think the problem is? If I take the same program and
compile it just using the c compiler(using cc) without the extern
"C"'s the program compiles and runs perfectly! Could it be a
pre-compiler directive causing this? In both instance, where it would
run or not run I used the exact same makefile. The only change I made
in the makefile was to change CC to cc or vice versa. Should I just
step through my code with dbx to find the problem?

Change tool chain, there is nothing wrong with the code.

Which platform/tools are you using?

Ian
Jul 22 '05 #3

P: n/a
Ian <no***@nowhere.com> wrote in message news:<10***************@drone5.qsi.net.nz>...
Christopher M. Lusardi wrote:

<snip>
WHAT CAN I DO
-------------
What do you think the problem is? If I take the same program and
compile it just using the c compiler(using cc) without the extern
"C"'s the program compiles and runs perfectly! Could it be a
pre-compiler directive causing this? In both instance, where it would
run or not run I used the exact same makefile. The only change I made
in the makefile was to change CC to cc or vice versa. Should I just
step through my code with dbx to find the problem?

Change tool chain, there is nothing wrong with the code.

Which platform/tools are you using?

Ian


There must be some-kind of confusion in my post. I posted the code with
the intention that it had virtually nothing to do with my problem. I posted
the code because I was unable to pin-point my "exact" problem. The posted
code was to show what calling C from C++ was all about in general, sorry.

The code works fine for me, but the original problem remains.

Question: What are the general problems encountered when trying to call
C code from C++ code? Is it all a matter of the complexity of the code?
Will simple non-complex code always work, and complex code not work? What
are the things I should be looking at?

Thank you,
Christopher Lusardi
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.