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

  • Thread starter Christopher M. Lusardi
  • Start date
C

Christopher M. Lusardi

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
 
E

E. Robert Tisdale

Christopher said:
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
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)
my_guess is 12580


It works just fine for me.
 
I

Ian

Christopher M. Lusardi wrote:

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
 
C

Christopher M. Lusardi

Ian said:
Christopher M. Lusardi wrote:


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top