I understand C is low-level, but I'd love the compiler to refuse to compile or
link a mixed C and C++ code that is not mixed correctly.

The story is: parts of the program are implemented in C, parts are in C++. The
functions in C++ are made callable from C using the "__attribute__((stdcall))"
feature, i.e. these are C-type functions implemented in C++. The main program
is in C, and attempts to call the C-style C++ function, which only computes a
number. The program crashes on exit from the main() function. It does not crash
if compiled using gcc/g++ version 3.3. However, removing the obsolete line "int
a = 1;" makes the program crash also when compiled using 3.3 version of the
compliers. Here are the sources:
------------------------------------------------------------
/* fnc.h - prototype of C++ function with C-style calling style  int f2(nt int)
*/

#ifdef __cplusplus
#define EXPORTCALL __attribute__((stdcall)) 
#else
#define EXPORTCALL 
#endif

#ifdef __cplusplus
extern "C" {
#endif

extern int EXPORTCALL f2(int a, int b);

#ifdef __cplusplus
}
#endif
---------------------------------------------------------------
/* fnmc.h - main C program that calls a C++ function   int f2(int, int) */

#include <stdio.h>
#include "fnc.h"

int main(int argc, char **argv)
{
  int a = 1;

  printf("f2(2,3)=%d\n", f2(2,3));
  printf("exiting...\n");
  return;
}
------------------------------------------------------------------
/* fnc.cpp - implementation of the C++ function   int f2(int, int), 
             using the C-calling semantics */

#include "fnc.h"      /* function prototype for f2() */

/* the function only returns the result */

int EXPORTCALL f2(int a, int b)
{
  return a+b;
}
------------------------------------------------------------------
# Makefile

GCC=gcc
GPP=g++

# GCC=gcc-3.3
# GPP=g++-3.3

all:    fn3

fn3:    fnc.cpp fnmc.c
        $(GPP) -v -save-temps -c fnc.cpp -g
        $(GCC) -v -save-temps -c fnmc.c -g
        $(GPP) -v -save-temps -o fn3 fnc.o fnmc.o -g

clean:
        rm -f *.o *.s *.i *.ii
        rm -f fn3 
------------------------------------------------------------------------
Here is the what is obtained when running:


petro...@student03:~/pasCPP2$ make clean
rm -f *.o *.s *.i *.ii
rm -f fn3 
petro...@student03:~/pasCPP2$ make
g++ -v -save-temps -c fnc.cpp -g
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v
--enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr
--enable-shared --with-system-zlib --libexecdir=/usr/lib
--without-included-gettext --enable-threads=posix --enable-nls
--with-gxx-include-dir=/usr/include/c++/4.2 --program-suffix=-4.2
--enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr
--enable-targets=all --enable-checking=release --build=i486-linux-gnu
--host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.2.4 (Ubuntu 4.2.4-1ubuntu3)
 /usr/lib/gcc/i486-linux-gnu/4.2.4/cc1plus -E -quiet -v -D_GNU_SOURCE fnc.cpp
-mtune=generic -fworking-directory -fpch-preprocess -o fnc.ii
ignoring nonexistent directory "/usr/local/include/i486-linux-gnu"
ignoring nonexistent directory
"/usr/lib/gcc/i486-linux-gnu/4.2.4/../../../../i486-linux-gnu/include"
ignoring nonexistent directory "/usr/include/i486-linux-gnu"
#include "..." search starts here:
#include <...> search starts here:
 /usr/include/c++/4.2
 /usr/include/c++/4.2/i486-linux-gnu
 /usr/include/c++/4.2/backward
 /usr/local/include
 /usr/lib/gcc/i486-linux-gnu/4.2.4/include
 /usr/include
End of search list.
 /usr/lib/gcc/i486-linux-gnu/4.2.4/cc1plus -fpreprocessed fnc.ii -quiet
-dumpbase fnc.cpp -mtune=generic -auxbase fnc -g -version -fstack-protector
-fstack-protector -o fnc.s
GNU C++ version 4.2.4 (Ubuntu 4.2.4-1ubuntu3) (i486-linux-gnu)
        compiled by GNU C version 4.2.4 (Ubuntu 4.2.4-1ubuntu3).
GGC heuristics: --param ggc-min-expand=98 --param ggc-min-heapsize=128358
Compiler executable checksum: cca9b7b48c023363b938f208576b99cc
 as --traditional-format -V -Qy -o fnc.o fnc.s
GNU assembler version 2.18.0 (i486-linux-gnu) using BFD version (GNU Binutils
for Ubuntu) 2.18.0.20080103
gcc -v -save-temps -c fnmc.c -g
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v
--enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr
--enable-shared --with-system-zlib --libexecdir=/usr/lib
--without-included-gettext --enable-threads=posix --enable-nls
--with-gxx-include-dir=/usr/include/c++/4.2 --program-suffix=-4.2
--enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr
--enable-targets=all --enable-checking=release --build=i486-linux-gnu
--host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.2.4 (Ubuntu 4.2.4-1ubuntu3)
 /usr/lib/gcc/i486-linux-gnu/4.2.4/cc1 -E -quiet -v fnmc.c -mtune=generic
-fworking-directory -fpch-preprocess -o fnmc.i
ignoring nonexistent directory "/usr/local/include/i486-linux-gnu"
ignoring nonexistent directory
"/usr/lib/gcc/i486-linux-gnu/4.2.4/../../../../i486-linux-gnu/include"
ignoring nonexistent directory "/usr/include/i486-linux-gnu"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/lib/gcc/i486-linux-gnu/4.2.4/include
 /usr/include
End of search list.
 /usr/lib/gcc/i486-linux-gnu/4.2.4/cc1 -fpreprocessed fnmc.i -quiet -dumpbase
fnmc.c -mtune=generic -auxbase fnmc -g -version -fstack-protector
-fstack-protector -o fnmc.s
GNU C version 4.2.4 (Ubuntu 4.2.4-1ubuntu3) (i486-linux-gnu)
        compiled by GNU C version 4.2.4 (Ubuntu 4.2.4-1ubuntu3).
GGC heuristics: --param ggc-min-expand=98 --param ggc-min-heapsize=128358
Compiler executable checksum: e2971cf2189271aeeb10133511ecea3a
 as --traditional-format -V -Qy -o fnmc.o fnmc.s
GNU assembler version 2.18.0 (i486-linux-gnu) using BFD version (GNU Binutils
for Ubuntu) 2.18.0.20080103
g++ -v -save-temps -o fn3 fnc.o fnmc.o -g
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v
--enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr
--enable-shared --with-system-zlib --libexecdir=/usr/lib
--without-included-gettext --enable-threads=posix --enable-nls
--with-gxx-include-dir=/usr/include/c++/4.2 --program-suffix=-4.2
--enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr
--enable-targets=all --enable-checking=release --build=i486-linux-gnu
--host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.2.4 (Ubuntu 4.2.4-1ubuntu3)
 /usr/lib/gcc/i486-linux-gnu/4.2.4/collect2 --eh-frame-hdr -m elf_i386
--hash-style=both -dynamic-linker /lib/ld-linux.so.2 -o fn3
/usr/lib/gcc/i486-linux-gnu/4.2.4/../../../../lib/crt1.o
/usr/lib/gcc/i486-linux-gnu/4.2.4/../../../../lib/crti.o
/usr/lib/gcc/i486-linux-gnu/4.2.4/crtbegin.o
-L/usr/lib/gcc/i486-linux-gnu/4.2.4 -L/usr/lib/gcc/i486-linux-gnu/4.2.4
-L/usr/lib/gcc/i486-linux-gnu/4.2.4/../../../../lib -L/lib/../lib
-L/usr/lib/../lib -L/usr/lib/gcc/i486-linux-gnu/4.2.4/../../.. fnc.o fnmc.o
-lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc
/usr/lib/gcc/i486-linux-gnu/4.2.4/crtend.o
/usr/lib/gcc/i486-linux-gnu/4.2.4/../../../../lib/crtn.o
petro...@student03:~/pasCPP2$ ./fn3 
f2(2,3)=5
exiting...
Segmentation fault
petro...@student03:~/pasCPP2$

------------------------------------------------------------------
I understand, I am probably doing the mixing of C and C++ incorrectly, although
I am not sure where (hints are welcome!), but still, would it be possible to
get the compiler/linker to produce a warning, if it is generating a code that
crashes? 

P.S. sorry for my previous unfinished post (accidentally clicked ENTER before
finishing the report)


-- 
           Summary: segmentation failed on correctly compiled mixed C and
                    C++ code
           Product: gcc
           Version: 4.2.4
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: pavel dot petrovic at gmail dot com
 GCC build triplet: i486-linux-gnu
  GCC host triplet: i486-linux-gnu
GCC target triplet: i486-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39662

Reply via email to