From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oa1-x33.google.com (mail-oa1-x33.google.com [IPv6:2001:4860:4864:20::33]) by sourceware.org (Postfix) with ESMTPS id F18DC3858C74 for ; Fri, 27 Jan 2023 13:48:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F18DC3858C74 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-oa1-x33.google.com with SMTP id 586e51a60fabf-15fe106c7c7so6467592fac.8 for ; Fri, 27 Jan 2023 05:48:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=It7QWEtQkG+5xd5UaRCPKOHjESCvmrJ7rdTrqyEP+og=; b=RSdrvepVu1jOQcotRQfbRGFSrBOGkKSKQuIMULtuigAWjlntdBuJ+CETEr+xZDeo2D mDQ0IpGFAoQ9PP/Xi4FCKw+AitWGrC2gejNwA8kF+F1YuqRKut9J6Xl/GiHNz1W9Dwha AgONJElM9OiS8WoeMNKLbLXLYJ37EbYcHBVTPtW9z15LI85LC0kQIbcdMiSUpdCQfzEp Lb9HfSmWu8I7ISug+PRDJcw7qsRJD8IthF0FaDJav1UeN+hjOG26q9G8zZ7UwexgqpGN kh5QSxd+CjRO0lBEiYM1Y0k48nJGe3fFOyNA/RGYMYgQKIpsVq3+BkP+TQ3syk2JXHD2 vkhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=It7QWEtQkG+5xd5UaRCPKOHjESCvmrJ7rdTrqyEP+og=; b=PR5zvRNFjXhV/5bAauAkmKzG1SEVWe+LaFTRJQisPWaZOugH/C11Ng002eEnWsTk3Y IjNttA5pJtHvYM7Bi1RKJTNF5X9kQev2FsS0QY8CygJtZruxaJ4uDbcIi3hFgczREJPi df+y4fTkIZc5Qwo8mEd/3ftQG32uayhM5Nnuk2FvuoeQuLhHquPfrTtW/AxO4Ot+TQg5 u6fk5JsDvprRkpo4S/Y5dHyehrpicw2N93u2rC++8PY0C6WDr0ONl7qmKYN51wvcj/Sv MnYKq9z5qyJT3fyGGQLcXjMLLPpahZ/0Ih4cJRU4zmC7FlsJF/bmuNHzz5QQqbRyPkUv ZBoA== X-Gm-Message-State: AFqh2kqHY4JXxLexJK5dsoR+5Fb+EfnLZTwmvYqqx8+NE7CmRkjGD3c2 2GXrRoEJQ0Mkfn0+4rAO5c1Uclu0ykG9WBA67nvY5FQdmnA= X-Google-Smtp-Source: AMrXdXsYFLGgywIVdwerSasyO+2JtT/DAa72QKvjCegHejgNX7yxTl0QcHM93Yw5umVaM8bAanEJXw56EHdwFmyU7JM= X-Received: by 2002:a05:6870:9e0e:b0:136:8a4d:f10e with SMTP id ps14-20020a0568709e0e00b001368a4df10emr2833457oab.243.1674827298949; Fri, 27 Jan 2023 05:48:18 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Tom Kacvinsky Date: Fri, 27 Jan 2023 08:48:08 -0500 Message-ID: Subject: Re: Correct way to provide a C callback function nside C++ To: gcc-help@gcc.gnu.org Content-Type: multipart/alternative; boundary="00000000000036cf6205f33f1e29" X-Spam-Status: No, score=0.1 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: --00000000000036cf6205f33f1e29 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, Jan 27, 2023 at 8:24 AM Pepe via Gcc-help wrote: > Hi there, > > I=E2=80=99m in an ongoing discussion about whether or not one should use = extern > =E2=80=9CC=E2=80=9D when defining a function that will be used as a callb= ack in a > statically linked C library. For example: > > c_func.h: > // =E2=80=A6 > void reg_callback(void (*fn)()); > // =E2=80=A6 > > cpp_impl.cpp: > // =E2=80=A6 > extern =E2=80=9CC=E2=80=9D { > #include =E2=80=9Cc_func.h=E2=80=9D > } > > // my callback function with internal linkage > namespace { > > extern =E2=80=9CC=E2=80=9D { > static void my_callback_A() { > // =E2=80=A6 > } > } // extern =E2=80=9CC=E2=80=9D > > void my_callback_B() { > // =E2=80=A6 > } > > } // namespace > > void do_something() { > reg_callback(my_callback_A); > reg_callback(my_callback_B); > } > > Both callbacks have internal linkage. Both work fine, and something like > my_callback_B is found in lots of code bases. > > In my opinion, using callback B is implementation defined behaviour, > because it is not guaranteed that C and C++ use the same calling > conventions. Therefore a function must adhere to the C calling conventions > to be used as a callback in a C library, which would be callback A. > You can do a callback (using function pointers) in C++ code. https://en.cppreference.com/w/cpp/language/pointer > I=E2=80=99ve been trying to find something definitive for days now, but t= o no > avail. Now I=E2=80=99m not sure what=E2=80=99s true or not. The counter a= rgument is the > following: The compiler should know reg_callback is a C function and make > sure that a given argument would either be valid or cause a compiler erro= r. > That sounds reasonable, so I would love to know how to do it properly for > future reference. Given we use gcc I was hoping to get a definitive answer > in this mailing list. Thanks a lot! > > Pepe > --00000000000036cf6205f33f1e29--