Home > database >  Is there any equivalent to index.js in C ?
Is there any equivalent to index.js in C ?

Time:12-05

In JavaScript, I can do import "/my-folder" and it will import /my-folder/index.js".
Is there some equivalent filename in C ? (so that #include "my-folder" will include my-folder/filename.fileext)?

CodePudding user response:

No, there is not equivalent to index.js in standard C . It would however be perfectly legal for a specific compiler to implement something like that, though I'm not aware of any compiler that does. Quoting from 19.2 [cpp.include] (N4659):

(1) A #include directive shall identify a header or source file that can be processed by the implementation.

(3) A preprocessing directive of the form

# include " q-char-sequence " new-line

causes the replacement of that directive by the entire contents of the source file identified by the specified sequence between the " delimiters. The named source file is searched for in an implementation-defined manner.

Emphasis mine.

I'm not sure what role index.js typically plays in JavaScript libraries, but if you're trying to implement a portable catch-all header for your library (so that the end users only need to include a single header instead of many), you'll just have to write your own header to serve that purpose. Headers named along the lines of my_folder/my_folder.h or my_folder/prelude.h would be common candidates.

CodePudding user response:

There is no such equivalent in C from the box, but you may emulate this.

  • Create the file "my-folder" with the content
    #include "_my-folder/filename.fileext"
    
  • Create the directory "_my-folder" and the file "filename.fileext" in that directory.
  • Use #include "my-folder". Do not forget to add patents of "my-folder" and "_my-folder" to include search paths.
  • Related