Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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". ;-)

In reply to Re^2: External (extra) files when using Inline::CPP by afoken
in thread External (extra) files when using Inline::CPP by cavac

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-06-25 07:52 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.