public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/39662] New: segmentation failed on correctly compiled mixed C and C++ code
@ 2009-04-06 12:21 pavel dot petrovic at gmail dot com
2009-04-06 12:24 ` [Bug c/39662] " pavel dot petrovic at gmail dot com
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: pavel dot petrovic at gmail dot com @ 2009-04-06 12:21 UTC (permalink / raw)
To: gcc-bugs
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:
petrovic@student03:~/pasCPP2$ make clean
rm -f *.o *.s *.i *.ii
rm -f fn3
petrovic@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
petrovic@student03:~/pasCPP2$ ./fn3
f2(2,3)=5
exiting...
Segmentation fault
petrovic@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
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c/39662] segmentation failed on correctly compiled mixed C and C++ code
2009-04-06 12:21 [Bug c/39662] New: segmentation failed on correctly compiled mixed C and C++ code pavel dot petrovic at gmail dot com
@ 2009-04-06 12:24 ` pavel dot petrovic at gmail dot com
2009-04-06 12:28 ` pavel dot petrovic at gmail dot com
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: pavel dot petrovic at gmail dot com @ 2009-04-06 12:24 UTC (permalink / raw)
To: gcc-bugs
------- Comment #1 from pavel dot petrovic at gmail dot com 2009-04-06 12:23 -------
Created an attachment (id=17594)
--> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=17594&action=view)
archive with all sources, and i. .ii, .s files
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39662
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c/39662] segmentation failed on correctly compiled mixed C and C++ code
2009-04-06 12:21 [Bug c/39662] New: segmentation failed on correctly compiled mixed C and C++ code pavel dot petrovic at gmail dot com
2009-04-06 12:24 ` [Bug c/39662] " pavel dot petrovic at gmail dot com
@ 2009-04-06 12:28 ` pavel dot petrovic at gmail dot com
2009-04-22 23:41 ` pinskia at gcc dot gnu dot org
2009-04-23 6:06 ` pavel dot petrovic at gmail dot com
3 siblings, 0 replies; 5+ messages in thread
From: pavel dot petrovic at gmail dot com @ 2009-04-06 12:28 UTC (permalink / raw)
To: gcc-bugs
------- Comment #2 from pavel dot petrovic at gmail dot com 2009-04-06 12:28 -------
*** Bug 39661 has been marked as a duplicate of this bug. ***
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39662
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c/39662] segmentation failed on correctly compiled mixed C and C++ code
2009-04-06 12:21 [Bug c/39662] New: segmentation failed on correctly compiled mixed C and C++ code pavel dot petrovic at gmail dot com
2009-04-06 12:24 ` [Bug c/39662] " pavel dot petrovic at gmail dot com
2009-04-06 12:28 ` pavel dot petrovic at gmail dot com
@ 2009-04-22 23:41 ` pinskia at gcc dot gnu dot org
2009-04-23 6:06 ` pavel dot petrovic at gmail dot com
3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2009-04-22 23:41 UTC (permalink / raw)
To: gcc-bugs
------- Comment #3 from pinskia at gcc dot gnu dot org 2009-04-22 23:41 -------
#ifdef __cplusplus
#define EXPORTCALL __attribute__((stdcall))
#else
#define EXPORTCALL
#endif
Is wrong, you need to use the same ABI for the function on both sides of the
call, that is either use stdcall on both sides (C and C++) or don't use it at
all.
--
pinskia at gcc dot gnu dot org changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
Resolution| |INVALID
Summary|segmentation failed on |segmentation failed on
|correctly compiled mixed C |correctly compiled mixed C
|and C++ code |and C++ code
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39662
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c/39662] segmentation failed on correctly compiled mixed C and C++ code
2009-04-06 12:21 [Bug c/39662] New: segmentation failed on correctly compiled mixed C and C++ code pavel dot petrovic at gmail dot com
` (2 preceding siblings ...)
2009-04-22 23:41 ` pinskia at gcc dot gnu dot org
@ 2009-04-23 6:06 ` pavel dot petrovic at gmail dot com
3 siblings, 0 replies; 5+ messages in thread
From: pavel dot petrovic at gmail dot com @ 2009-04-23 6:06 UTC (permalink / raw)
To: gcc-bugs
------- Comment #4 from pavel dot petrovic at gmail dot com 2009-04-23 06:06 -------
Thanks! it works. I was confused by this document:
ftp://ftp.freepascal.org/pub/fpc/docs-pdf/CinFreePascal.pdf which seems to
claim the opposite.
--
pavel dot petrovic at gmail dot com changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |pinskia at gcc dot gnu dot
| |org
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39662
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2009-04-23 6:06 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-04-06 12:21 [Bug c/39662] New: segmentation failed on correctly compiled mixed C and C++ code pavel dot petrovic at gmail dot com
2009-04-06 12:24 ` [Bug c/39662] " pavel dot petrovic at gmail dot com
2009-04-06 12:28 ` pavel dot petrovic at gmail dot com
2009-04-22 23:41 ` pinskia at gcc dot gnu dot org
2009-04-23 6:06 ` pavel dot petrovic at gmail dot com
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).