Search code examples
cubuntuextern

Error compiling code in c for cross-platform (Linux)


The project code I need to compile is in C. The project compiles correctly in Visual Studio 2015 but I need to migrate it to linux (Ubuntu) and it gives me error of the following type:

/jpeglib8.h:1011:8: error: expected '=', ',', ';', 'asm' or 'attribute' before 'void' EXTERN (void) jpeg_CreateCompress JPP ((j_compress_ptr cinfo,

1005    #define jpeg_create_compress(cinfo) \
1006        jpeg_CreateCompress((cinfo), JPEG_LIB_VERSION, \
1007                (size_t) sizeof(struct jpeg_compress_struct))
1008    #define jpeg_create_decompress(cinfo) \
1009        jpeg_CreateDecompress((cinfo), JPEG_LIB_VERSION, \
1010                  (size_t) sizeof(struct jpeg_decompress_struct))
1011    EXTERN(void) jpeg_CreateCompress JPP((j_compress_ptr cinfo,
1012                          int version, size_t structsize));
1013    EXTERN(void) jpeg_CreateDecompress JPP((j_decompress_ptr cinfo,
1014                    int version, size_t structsize));

Apparently there is no syntax error. I will really appreciate the help, thank you.


Solution

  • The EXTERN macro is often used to declare variables (allocate storage) or just define them (make them known). It works like this:

    // main.c
    #define EXTERN
    #include "myinclude.h"
    
    // sub-module.c
    #include "myinclude.h"
    
    // myinclude.h
    #ifndef EXTERN
    #define EXTERN extern
    #endif
    EXTERN int myvar;
    EXTERN void do_something(int a);
    

    In the above, when myinclude.h is inluded in main.c, the directive EXTERN is set to nothing and so the variable int myvar will be allocated. In all other modules that include it, it will be set to extern and so only define the variable.

    For functions this is not necessary anymore with the advent of prototypes.