0

I plan on building several libraries following the FIPS standards (hashing, encryption). I want to have several batch/bash scripts that will automate the building process of these libraries for every desktop platforms (Windows, Mac, Linux). I plan on using MinGW's port of gcc on Windows and simply gcc on Linux or Mac.

My code for the library is the following:

hash.h

#ifndef HASHH
#define HASHH

#ifdef STATIC
 
    #define ADDAPI
    #define ADDCALL
    #define EXTERN extern

#else

    #define EXTERN

    #ifdef _WIN32

        #ifdef BUILD
            #define ADDAPI __declspec(dllexport)
        #else
            #define ADDAPI __declspec(dllimport)
        #endif

        #define ADDCALL __cdecl
  
    #else

        #define ADDAPI
        #define ADDCALL

    #endif

#endif

/*** The sha3-256 hash function.
@param buffer The buffer containing the data to be hashed.
@param length The length of the buffer.
@param digest The hash of the data in buffer.
*/
ADDAPI EXTERN int ADDCALL sha3_256(unsigned char* buf, unsigned long len, unsigned char* digest);

#endif

hash.c

#include "hash.h"

int sha3_256(unsigned char* buf, unsigned long len, unsigned char* digest){
    return len * 2;
}

For DLL target on Windows I do:

gcc -c -D BUILD source\hash.c -oobjects\hash.o
ld -s -shared objects\hash.o -ohash.dll

For static target I do:

gcc -c -D STATIC source\hash.c -oobjects\hash.o
ar rcs hash.lib objects\hash.o

Everything works fine for building but when I use the header in a program the ADDAPI and ADDCALL macros are never defined for static building. This is due to the STATIC macro never initialized. I would like that this same header works for both building and using in client apps. I would also like that the macros allow to build both for dll target than for static lib target without the client having to pass a macro on the command line for it to work.

Do you have any idea if this is possible? If it is, how would you go about doing this?

With the above code both building static and dll works but for client only dll works because for static I need to pass the STATIC macro on the command line. I want an option to prevent that.

user123
  • 2,510
  • 2
  • 6
  • 20
  • 1
    My advice: if macros do not work use option -E to see the preprocessed C sources – 0___________ Sep 30 '21 at 07:37
  • Yes I did use the -E option for checking. The problem is not with macros. I get the logic and everything. I just need a way to be able to compile both a static library and a dynamic library both with the same header (without having to pass -D macros when compiling client apps). – user123 Sep 30 '21 at 07:41
  • What happens if you remove `__declspec(dllimport)`? – ssbssa Sep 30 '21 at 10:42
  • The symbol becomes undefined when linking client apps. – user123 Sep 30 '21 at 10:43
  • Anyway I decided to just use 2 separate headers for the job. – user123 Sep 30 '21 at 10:44
  • Unless someone got a solution. – user123 Sep 30 '21 at 10:46
  • 1
    The file (excepting some minor things) is *OK*. It does what you said it should. The missing part is when building the client app to define the flag (let the compiler know you want to build with the static lib version). For a cleaner version of the file, check [\[SO\]: Linker error when calling a C function from C++ code in different VS2010 project (@CristiFati's answer)](https://stackoverflow.com/questions/30581837/linker-error-when-calling-a-c-function-from-c-code-in-different-vs2010-project/30583411#30583411). – CristiFati Sep 30 '21 at 13:19

0 Answers0