From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Omar Polo Newsgroups: gmane.emacs.help Subject: Re: C Hello World Emacs module Date: Tue, 05 Oct 2021 13:26:21 +0200 Message-ID: <87lf373dq3.fsf@omarpolo.com> References: <87mtnn23a8.fsf@zoho.eu> <87o8834uyx.fsf@omarpolo.com> <871r4z204q.fsf@zoho.eu> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="14278"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mu4e 1.6.6; emacs 28.0.50 Cc: help-gnu-emacs@gnu.org To: Emanuel Berg Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Tue Oct 05 13:37:37 2021 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mXilN-0003YY-2H for geh-help-gnu-emacs@m.gmane-mx.org; Tue, 05 Oct 2021 13:37:37 +0200 Original-Received: from localhost ([::1]:48642 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mXilL-0001KH-VV for geh-help-gnu-emacs@m.gmane-mx.org; Tue, 05 Oct 2021 07:37:35 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:36732) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mXikW-0001JC-Hm for help-gnu-emacs@gnu.org; Tue, 05 Oct 2021 07:36:46 -0400 Original-Received: from mail.omarpolo.com ([144.91.116.244]:62713) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mXikT-0000iL-QC for help-gnu-emacs@gnu.org; Tue, 05 Oct 2021 07:36:44 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=omarpolo.com; s=20200327; t=1633433798; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Hrf15lZNBhlp6bQi08R45Ijs3Nx5OOXh0pLdUMuzIaI=; b=QSWfPcrFltFwp9OmWXdDu8iccS2JGxgQFfFhT5T+EuBw3gEHrOrO6wT5MDOYiBKoveKkaf arE8o5JeiJnGDdHxEKGTLZ/FbKC+aZkPws/4/pL8c7sTeotBNxycj0EIgwKZR6tB/vqscn qIdeM0CHgT0kD3O1wmcrXPmg8PhHaik= Original-Received: from localhost (host-79-24-9-175.retail.telecomitalia.it [79.24.9.175]) by mail.omarpolo.com (OpenSMTPD) with ESMTPSA id 1b9a91b9 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Tue, 5 Oct 2021 13:36:38 +0200 (CEST) Original-Received: from venera (localhost [127.0.0.1]) by localhost (OpenSMTPD) with ESMTP id f3c0c35e; Tue, 5 Oct 2021 13:36:36 +0200 (CEST) In-reply-to: <871r4z204q.fsf@zoho.eu> Received-SPF: pass client-ip=144.91.116.244; envelope-from=op@omarpolo.com; helo=mail.omarpolo.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:133569 Archived-At: Emanuel Berg via Users list for the GNU Emacs text editor writes: > Omar Polo wrote: > >>> Is there a minimal working example, in the LaTeX lingo, for >>> C modules in Emacs? >> >> Other than in the manual, I've found this article by skeeto >> to be really helpful in understanding how modules works: >> >> https://nullprogram.com/blog/2016/11/05/ >> >> The first bit of code is how to call `message' with "Hello, >> world!" as argument. > > OK, thanks, that one step closer, there's this > > #include "emacs-module.h" > > int plugin_is_GPL_compatible; > > int emacs_module_init(struct emacs_runtime *ert) { > emacs_env *env =3D ert->get_environment(ert); > emacs_value message =3D env->intern(env, "message"); > const char hi[] =3D "Hello, world!"; > emacs_value string =3D env->make_string(env, hi, sizeof(hi) - 1); > env->funcall(env, message, 1, &string); > return 0; > } > > In a real module, it=E2=80=99s common to create function objects for > native functions, then fetch the fset symbol and make a Lisp > call on it to bind the newly-created function object to > a name. You=E2=80=99ll see this in action later. > > But still missing is the command(s) and options to compile and > instructions how to make it visible from Emacs ... > > Also I'd like to have the example as it would look "[i]n > a real module" ... so it shouldn't just be a MWE, it should be > a MCAWE (Minimal Correct and Applicable Working Example) > > :) There's a Makefile linked in the article, which even if it has a .POSIX target is just a GNU Makefile, but anyway. https://github.com/skeeto/joymacs/blob/master/Makefile Assuming you're on some UNIX flavors, you need to create a shared library. This means that you need to compile every object file with -fPIC and then bundle 'em together with something like cc -shared -fPIC -o your-module.so obj1.o obj2.o obj3.o ... (where obj1.o, obj2.o etc are your compiled C files, for e.g. cc -fPIC -c obj1.c -o obj1.o ) To load it, IIRC is mentioned in the manual, you just drop the .so file inside `load-path' and `require' it like it were a .el/.elc file. How structure it heavily depends on what you want to do. Usually one wants to export some functions from C to elisp, but you may want to do something else. In the linked article the first snippet (the one you quoted) shows how to translate (message "Hello, world!") in a C module, later examples shows how to make functions callable from elisp. Another example is emacs-libpq, the bindings for the postgresql library. It is of course CAWE (correct and applicable working example), don't know if I would consider it minimal thought. https://github.com/anse1/emacs-libpq/blob/master/pq-core.c#L354 They define a handy DEFUN macro to prettify the process of registering functions. In the end, how to do it heavily depends how what you want to do ;)