0

I am trying to include a compiled library into a C project on a Nordic nrf52840. Below (as far as I understand) is a way to link to some of the methods foo and bar within the .lib file for the rest of the project. When Trying to compile this with Segger Embedded Studio I get the following expected '=', ',', ';', 'asm' or '__attribute__' before 'int' error with the following code snippet:

#ifndef _FOOBAR_SERVICE_H_
#define _FOOBAR_SERVICE_H_

#if (defined(__linux__) || defined(__APPLE__) || defined(ARDUINO) || 
defined(__MSP430FR5969__))
#define IMPORT __attribute__ ((visibility ("default")))
#define EXPORT __attribute__ ((visibility ("default")))
#define LOCAL  __attribute__ ((visibility ("hidden")))
#elif defined(_WIN32)
#define EXPORT __declspec(dllexport)
#endif

#include <stdbool.h>

#ifdef __cplusplus

extern "C"
{
#endif

EXPORT int ble_foo(unsigned char  *a, unsigned char *buffer);  //<--(error)

EXPORT int ble_bar(unsigned char  *b, unsigned char *buffer);  //<--(same error)

#ifdef __cplusplus
}
#endif
#endif /* _FOOBAR_SERVICE_H_ */

The above is #include "foobar_ble.h" included in my main.c file.

Part of it might be my misunderstanding of extern "C" I believe it to be a way of compiling the C code. I believe the #ifdef __cplusplus is checking to compile as c++ so would this mean that extern "C" is not even utilized within a C environment?

Also, I cannot seem to find a good explanation of the EXPORT keyword within C. This could also be a source of my problems.

Tl;dr: Too dumb, too many questions, need help. Plz & thanks.

luckyging3r
  • 3,047
  • 3
  • 18
  • 37
  • 1
    `EXPORT` isn't a language keyword. It's usually a preprocessor macro you need to define (or that should be defined for you by the relevant library header). – François Andrieux Aug 31 '18 at 20:46
  • Ahh ok. I see what you're saying. Let me update my question – luckyging3r Aug 31 '18 at 20:47
  • 1
    Looks to me like your compiler isn't one that is supported by the thing you are trying to compile. As a result, it doesn't hit any of those preprocessor conditions and never defines `EXPORT` or it's hitting one that isn't appropriate and expands to something your compiler can't handle. – François Andrieux Aug 31 '18 at 20:50
  • That might be the case. I think the library I have was compiled for the Ti cc2640 so that is probably the case. I'll look into that. Thanks! – luckyging3r Aug 31 '18 at 20:52
  • 2
    This doesn’t address the question, but names that begin with an underscore followed by a capital letter (`_FOOBAR_SERVICE_H_`) and names that contain two consecutive underscores are reserved for use by the implementation. Don’t use them in your code. – Pete Becker Aug 31 '18 at 21:29
  • @PeteBecker by "reserved for use by the implementation" would you be referring to something like this? https://stackoverflow.com/a/37326614/5184092 or https://stackoverflow.com/a/228797/5184092 (the latter probably being more thorough) – luckyging3r Aug 31 '18 at 21:49

1 Answers1

3

I think the problem is that EXPORT in your case isn't defined to anything. So it will just stay in the source code and cause a syntax error. It is probably intended to be defined to something like __declspec(dllexport) on static libraries, and to nothing (empty string) for static usage.

You should be able to fix it by defining EXPORT to an empty string. Depending on the library there might be a place for that in some configuration header file. Otherwise you can also use a define on compiler invocation level, but that might not be preferable, since it removes all EXPORT words from the source code.

Matthias247
  • 9,836
  • 1
  • 20
  • 29