Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: External (extra) files when using Inline::CPP

by NERDVANA (Curate)
on Apr 18, 2024 at 18:00 UTC ( [id://11158960]=note: print w/replies, xml ) Need Help??


in reply to External (extra) files when using Inline::CPP

You can always just #include "filename.c" from the inline C code. The only reason to compile separate units is to prevent symbol collisions, or enable parallel compilation, and in a small project those aren't really problems.

This is what I do in OpenGL::Sandbox For that matter, the C code got so large I wanted syntax highlighting, so I put it all into a separate file anyway. Note that OpenGL::Sandbox has been retooled with Inline::Module so that when installed from CPAN, it compiles once at install time like a regular XS module. But, while developing you still get the advantage of automatic recompiles each time you load it.

Replies are listed 'Best First'.
Re^2: External (extra) files when using Inline::CPP
by afoken (Chancellor) on Apr 19, 2024 at 07:51 UTC
    You can always just #include "filename.c" from the inline C code.

    Most times, yes. But there are differences:

    If you use #include "filename.c", static functions and variables in filename.c suddenly become visible in the inline C code, whereas they are separate and not visible from inline C code if you just link with filename.o. That means you can't have one static type1 foo(...) in filename.c and another static type2 foo(...) in inline C code if you use #include "filename.c", but it works fine if you compile and filename.c to filename.o and just link with filename.o.

    Static variables are even worse. Two variables with equal name and type (one from inline C, one from filename.c) suddenly become one:

    $ cat a.c #include "b.c" static int foo; int main(int argc, char ** argv) { foo=argc; // dummy code return foo; // dummy code } $ cat b.c static int foo; $ cc -Wall -pedantic a.c $

    Note: no errors, no warnings.

    $ cat a.c #include "b.c" static int foo; int main(int argc, char ** argv) { foo=argc; // dummy code return foo; // dummy code } $ cat b.c static int foo = 42; $ cc -Wall -pedantic a.c $

    No errors, no warnings when initialized in one file.

    $ cat a.c #include "b.c" static int foo = 42; int main(int argc, char ** argv) { foo=argc; // dummy code return foo; // dummy code } $ cat b.c static int foo = 42; $ cc -Wall -pedantic a.c a.c:3:12: error: redefinition of ‘foo’ 3 | static int foo = 42; | ^~~ In file included from a.c:1: b.c:1:12: note: previous definition of ‘foo’ with type ‘int’ 1 | static int foo = 42; | ^~~ b.c:1:12: warning: ‘foo’ defined but not used [-Wunused-variable] $

    Both variables must be initialized to cause an error.

    $ cc --version cc (Debian 12.2.0-14) 12.2.0 Copyright (C) 2022 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There i +s NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PUR +POSE. $

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      That's a very nice illustration of my statement "The only reason to compile separate units is to prevent symbol collisions", but my point was that in a small enough amount of code written by one person, name collisions are not usually a problem.
Re^2: External (extra) files when using Inline::CPP
by etj (Curate) on Apr 20, 2024 at 16:14 UTC
    My workflow for these things is to routinely use make test (I really wish this site had Markdown so I could use ``). That does the rebuild for me. For things other than tests, make && perl -Mblib script works great.
        Cough...

        --> Wikisyntax for the Monastery

        Not markdown but could be adapted to be markdown(ish)

        For instance it has '...' for inline code.

        On a side note: I'm writing most of my posts here with my mobile, because I can use this markup

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        see Wikisyntax for the Monastery

Re^2: External (extra) files when using Inline::CPP
by etj (Curate) on Apr 20, 2024 at 16:21 UTC
    By the way, I'm now the maintainer of OpenGL::Modern, so if you'd like that to work differently from how it does now, please say so! I think Perl OpenGL is due somewhat of a refresh to support OpenGL versions made in the last couple of decades, so I'm very open to ideas on how to best execute that.
      I haven't really found time for any OpenGL work the last few years. My OpenGL::Sandbox has some really cool wrappers around Shaders and Programs which would be nice to have upstream for wider usability, but I can't say anything is missing really.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11158960]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2024-06-25 08:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.