From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 8021 invoked by alias); 9 Aug 2014 19:51:08 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 8001 invoked by uid 89); 9 Aug 2014 19:51:04 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=0.1 required=5.0 tests=BAYES_50,RP_MATCHES_RCVD,SPF_HELO_PASS,SPF_PASS autolearn=ham version=3.3.2 X-HELO: da.nameserverus2.com Received: from da.nameserverus2.com (HELO da.nameserverus2.com) (86.111.247.88) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Sat, 09 Aug 2014 19:51:03 +0000 Received: from sir10000 by da.nameserverus2.com with local (Exim 4.83) (envelope-from ) id 1XGCfJ-0000Lw-AD for gcc-help@gcc.gnu.org; Sat, 09 Aug 2014 21:50:57 +0200 Received: from 89.75.51.68 ([89.75.51.68]) by sirzooro.prohost.pl (Horde Framework) with HTTP; Sat, 09 Aug 2014 21:50:57 +0200 Date: Sat, 09 Aug 2014 19:51:00 -0000 Message-ID: <20140809215057.Horde.vyEDuGF06pHW1H_WOtKpyw1@sirzooro.prohost.pl> From: daniel@poradnik-webmastera.com To: gcc-help@gcc.gnu.org Subject: C++ name mangling in C User-Agent: Internet Messaging Program (IMP) H5 (6.2.1) Content-Type: text/plain; charset=UTF-8; format=flowed; DelSp=Yes MIME-Version: 1.0 Content-Disposition: inline X-SW-Source: 2014-08/txt/msg00071.txt.bz2 Hi, C language does not use name mangling like C++. This can lead to subtle bugs, when function prototype is declared differently in different files. Simple example: /* file1.c */ int test(int x, int y) { return y; } /* file2.c */ #include extern int test(int x); int main() { int n = test(2); printf("n = %d\n", n); return 0; } When this code is compiled using C++ compiler, such error will be reported at linking phase as "undefined reference to 'test(int)'". Unfortunately in C compilation and linking will succeed, so bug will appear at runtime. But such bugs may be very hard to find. My code base is too big to clean up all this mess and move declarations to header files manually in relatively short time. Therefore I was looking for a way to detect such bugs with some tool. I thought about forcing C++ mangling when compiling C code, but looks that gcc does not have any command line option to this. Please correct me if I am wrong. If there is no such option, I would like to open an enhancement to add it. I thought about it for some time and looks that this new option should do 3 things: enable C++ name mangling, enable extern "C" directive and define the __cplusplus macro. Any comments on this proposal is also welcome. Regards, Daniel