Linking C++ code to C in Borland C++ 3.1 compiler - c

Part of my FYP is to write code for a very old game(Wolfenstein-3D). It requires the use of the Borland C++ v3.1 compiler. This is the code I currently have but it's giving an error in the Borland compiler. Any ideas?
Error In Compiler:
Neuron.h
#ifdef __cplusplus // only actually define the class if this is C++
class Neuron {
public:
void foo();
int bar(int x, int y);
};
#else // C doesn't know about classes, just say it's a struct
typedef struct Neuron Neuron;
#endif
// access functions
#ifdef __cplusplus
#define EXPORT_C extern "C"
#else
#define EXPORT_C
#endif
EXPORT_C Neuron* NeuronNew(void);
EXPORT_C void NeuronDelete(Neuron* n);
EXPORT_C void NeuronFoo(Neuron* n);
EXPORT_C int NeuronBar(Neuron* n, int x, int y);
Neuron.cpp
#include "NEURON.h"
void Neuron::foo() {
}
int Neuron::bar(int x, int y) {
return x+y;
}
EXPORT_C Neuron* NeuronNew(void) {
return new Neuron();
}
EXPORT_C void NeuronDelete(Neuron* n) {
delete n;
}
EXPORT_C void NeuronFoo(Neuron* n) {
return n->foo();
}
EXPORT_C int NeuronBar(Neuron* n, int x, int y) {
return n->bar(x, y);
}
Usage in C source file
#include "NEURON.h"
...
void GameLoop (void)
{
...
Neuron* m = NeuronNew();
NeuronFoo(m);
NeuronDelete(m);
...
}
My assumption is that even though the compiler is a C++ compiler there's something 'new' in the C++ code that the compiler can't handle

The error message looks very much like the one you get from other compilers when they can't build the .cpp file for your class. It's not complaining about NeuronNew, but about _Neuron_new (note lowercase 'n' and extra underscore), so that's likely what Borland names the constructor/destructor?
Is it successfully compiling the .cpp and as C++? Is the file suffix mapping hooked up for those in the compiler? Have you added invalid code to the #ifdef __cplusplus line to verify that is ever defined, and not always defined (as 0 or 1)? Are you using the same case for all includes, file names and in the Makefile/project so they can be found?
Oh, have you tried doing:
typedef struct Neuron* NeuronPtr;
and then using NeuronPtr instead of Neuron* in the C wrapper? C++ Compiler shouldn't care (as long as you do a typedef class Neuron* NeuronPtr; in the __cplusplus part), but it means that it might no longer try to resolve the forward-declared struct in any C code.

Related

Can I include a library in a function residing in said library?

I am somewhat new to programming and I'm trying to make a project where I make my own function library. Inside of said library I have one function that uses two other functions in it too. Can I #include "library.h" on the function inside library.h to use the other two functions? Thanks in advance =)
Example of some code:
//mylib.h
int func1(int x);
int func2(int x);
int func3();
in func3.c:
int func3()
{
int x1 = func1(int x);
int x2 = func2(int x);
return(x1 + x2);
}
//func3 needs func1 and func2 which are defined in the same library
hope this makes any sense
Of course you can, and you should. It is common sense to include the header file containing the public API of some code in the source file(s) implementing that code. This way the compiler checks for you that the prototypes (and other stuff like declarations) match their counterparts in the implementation source.
For example, the header file "mylib.h" is:
#if !defined(MY_LIB_H)
#define MY_LIB_H
int func1(int x);
int func2(int x);
int func3(int x1, int x2);
#endif
This header file contains the so-called header-guard. It helps against multiple definitions if the header file is include multipl times, for example if you include it in multiple other header files, which are included in some source file.
And its implementation "mylib.c":
#include "mylib.h"
int func1(int x) {
return x;
}
int func2(int x) {
return -x;
}
int func3(int x1, int x2) {
return func1(x1) * func2(x2);
}

Right usage of ifdef to write the same function for different type of values, without writing it more than once

Im doing my homework, and I have hard time figuring out one thing. I'm suppose to write BST that can work with integer or with float.
And for such and practicing we have different header files for integer, float and the tree itself.
I'm trying to create a way for a function to run based of which type I'm currently working with [int/float] without having to write it twice. so I got something like this:
void addItemToTree(BST* bst, void* val)
{
#ifdef IsInt
addItemToTreeRec(bst->head, *((int*)val));
#else
addItemToTreeRec(bst->head, *((float*)val));
#endif
}
so far no problems, it leads to:
#ifdef IsInt
void addItemToTreeRec(node* my_node, int val)
#else
void addItemToTreeRec(node* my_node, float val)
#endif
{
-- function itself ---
}
but the problem is, the header file that contains all integer related struct and declaration do not recognize my function while the float header file does.
I figured it has to do something with the way I lay the ifdef down but I cannot seems to solve it without having to copy the whole code and put it before #else
Note: Preprocessor macros expand before compilation. You can not change the function definition and invoke different versions of the function at run-time.
If this is the thing you're looking for nonetheless, you can use #ifdef/#else directives in the parameter list to choose which type the second parameter should be without actually to define different functions:
#define IsInt
void addItemToTreeRec (node* my_node, // Function declaration addItemToTreeRec.
#ifdef IsInt
int
#else
float
#endif
val
);
void addItemToTreeRec (node* my_node, // Function definition addItemToTreeRec.
#ifdef IsInt
int
#else
float
#endif
val
)
{
#ifdef IsInt
// Code if val is of type int.
#else
// Code if val is of type float.
#endif
}
void addItemToTree ( BST* bst, // Function declaration addItemToTree.
#ifdef IsInt
int
#else
float
#endif
val
);
void addItemToTree ( BST* bst, // Function definition addItemToTree.
#ifdef IsInt
int
#else
float
#endif
val
)
{
addItemToTreeRec (bst->head,
#ifdef IsInt
*((int*)val)
#else
*((float*)val)
#endif
);
}
Note: This is not necessary to be seen as subsequent code. You can bring the declarations into specific header files, as well as the definitions into different source files, but addItemToTreeRec needs to be at least forward declared before addItemToTree because it is used in addItemToTree.

Trouble with using a special type in two header files. (C code)

I'm having trouble using a special type I made in two header files. One header files defines the type, the other one uses it in an extern variable, and a function.
"newtype.h"
typedef struct {
double i, s;
} newtype_t;
newtype_t this(){}
newtype_t that(){}
Now I have another header with a variable and function:
"newfuncs.h"
extern newtype_t c;
newtype_t divide(double d, double e);
I'm getting:
unknown type name 'newtype_t' //inside of "newfuncs.h"
This new header "newfuncs.h" is an addition to already working code that utilizes the new type and functions from "newtype.h".
I'm using this newtype_t inside of newfuncs.h.
I tried to #include "newtype.h" but I get a multitude of errors involving "conflicting types" in my .c files.
If a header use a type declared in another header, it should include it.
To prevent multiple declaration problem, you should add a guard in your header files:
// file newtype.h
#ifndef NEWTYPE_H // the guard
#define NEWTYPE_H
// all header stuff goes here
typedef int my_type;
#endif // end of guard
By doing this, each type or function will only be defined once. Each guard macro should be unique, you can use the file name to choose its name.
Some compiler (like cl) have a special comment to prevent multiple inclusion: #pragma once
did you included the header files in the right order?
I just tested it and it works
test.c
#include "newtype.h"
#include "newfuncs.h"
#include <stdio.h>
int main(){
double d = 4.0;
double e = 2.0;
c = divide(d, e);
printf("HELLO WORLD\n");
printf("%lf", c.i);
printf("%lf", c.s);
return 0;
}
newtype_t divide(double d, double e){
newtype_t x;
x.i = d;
x.s = e;
return x;
}
newtype.h
#ifndef NEWTYPE_H
#define NEWTYPE_H
typedef struct {
double i, s;
} newtype_t;
#endif
newfuncs.h
#ifndef NEWFUNCS_H
#define NEWFUNCS_H
newtype_t c;
newtype_t divide(double d, double e);
#endif
but even without the define it works for me (gcc 5.3.1).

Declare function as non-static and implement as static inline

I want to write different implementations for my function, some inline and some not. Thus, I want to declare the function as:
// MyHeader.h
int myFunc(void);
#if DO_INLINE
static inline int myFunc(void) { return 42; }
#endif
And then also have:
// MySource.c
#if !DO_INLINE
#include "myHeader.h"
int myFunc(void) { return 42; }
#endif
I'll specify DO_INLINE at compile time.
MSVC has no problems with this, but GCC (4.1.1) complains that I'm declaring a static function after I've already declared it as non-static. If I remove the static qualifier, and #include "MyHeader.h" from more than one compilation unit, it will complain about multiple definitions. (As if the inline functions are extern.) I don't quite understand why the compiler has problems with this.
I think this should be pretty obvious and unambiguous:
int myFunc(void);
static inline int myFunc(void) { return 42; }
It shouldn't require the declaration to be static.
That said, there is a solution to my problem that I'm trying very hard to avoid:
#if DO_INLINE
#define MAYBE_STATIC static
#else
#define MAYBE_STATIC
#endif
MAYBE_STATIC int myFunc(void);
EDIT: Here is a more realistic use case for this: http://codepad.org/OkC0Su3v
This header.h should work:
// MyHeader.h
#if DO_INLINE
static inline int myFunc(void) { return 42; }
#else
int myFunc(void);
#endif
Figured it out closely enough. The implementation should be defined as "extern inline" instead:
// MyHeader.h
int myFunc(void);
#if DO_INLINE
extern inline int myFunc(void) { return 42; }
#endif
The compiler will inline this function where it sees fit, but still compile it once as a function, to make it available for linking. That part I don't need, but it doesn't really hurt.

Designing an API with compile-time option to remove first parameter to most functions and use a global

I'm trying to design a portable API in ANSI C89/ISO C90 to access a wireless networking device on a serial interface. The library will have multiple network layers, and various versions need to run on embedded devices as small as an 8-bit micro with 32K of code and 2K of data, on up to embedded devices with a megabyte or more of code and data.
In most cases, the target processor will have a single network interface and I'll want to use a single global structure with all state information for that device. I don't want to pass a pointer to that structure through the network layers.
In a few cases (e.g., device with more resources that needs to live on two networks) I will interface to multiple devices, each with their own global state, and will need to pass a pointer to that state (or an index to a state array) through the layers.
I came up with two possible solutions, but neither one is particularly pretty. Keep in mind that the full driver will potentially be 20,000 lines or more, cover multiple files, and contain hundreds of functions.
The first solution requires a macro that discards the first parameter for every function that needs to access the global state:
// network.h
typedef struct dev_t {
int var;
long othervar;
char name[20];
} dev_t;
#ifdef IF_MULTI
#define foo_function( x, a, b, c) _foo_function( x, a, b, c)
#define bar_function( x) _bar_function( x)
#else
extern dev_t DEV;
#define IFACE (&DEV)
#define foo_function( x, a, b, c) _foo_function( a, b, c)
#define bar_function( x) _bar_function( )
#endif
int bar_function( dev_t *IFACE);
int foo_function( dev_t *IFACE, int a, long b, char *c);
// network.c
#ifndef IF_MULTI
dev_t DEV;
#endif
int bar_function( dev_t *IFACE)
{
memset( IFACE, 0, sizeof *IFACE);
return 0;
}
int foo_function( dev_t *IFACE, int a, long b, char *c)
{
bar_function( IFACE);
IFACE->var = a;
IFACE->othervar = b;
strcpy( IFACE->name, c);
return 0;
}
The second solution defines macros to use in the function declarations:
// network.h
typedef struct dev_t {
int var;
long othervar;
char name[20];
} dev_t;
#ifdef IF_MULTI
#define DEV_PARAM_ONLY dev_t *IFACE
#define DEV_PARAM DEV_PARAM_ONLY,
#else
extern dev_t DEV;
#define IFACE (&DEV)
#define DEV_PARAM_ONLY void
#define DEV_PARAM
#endif
int bar_function( DEV_PARAM_ONLY);
// I don't like the missing comma between DEV_PARAM and arg2...
int foo_function( DEV_PARAM int a, long b, char *c);
// network.c
#ifndef IF_MULTI
dev_t DEV;
#endif
int bar_function( DEV_PARAM_ONLY)
{
memset( IFACE, 0, sizeof *IFACE);
return 0;
}
int foo_function( DEV_PARAM int a, long b, char *c)
{
bar_function( IFACE);
IFACE->var = a;
IFACE->othervar = b;
strcpy( IFACE->name, c);
return 0;
}
The C code to access either method remains the same:
// multi.c - example of multiple interfaces
#define IF_MULTI
#include "network.h"
dev_t if0, if1;
int main()
{
foo_function( &if0, -1, 3.1415926, "public");
foo_function( &if1, 42, 3.1415926, "private");
return 0;
}
// single.c - example of a single interface
#include "network.h"
int main()
{
foo_function( 11, 1.0, "network");
return 0;
}
Is there a cleaner method that I haven't figured out? I lean toward the second since it should be easier to maintain, and it's clearer that there's some macro magic in the parameters to the function. Also, the first method requires prefixing the function names with "_" when I want to use them as function pointers.
I really do want to remove the parameter in the "single interface" case to eliminate unnecessary code to push the parameter onto the stack, and to allow the function to access the first "real" parameter in a register instead of loading it from the stack. And, if at all possible, I don't want to have to maintain two separate codebases.
Thoughts? Ideas? Examples of something similar in existing code?
(Note that using C++ isn't an option, since some of the planned targets don't have a C++ compiler available.)
I like your second solution. I just prefer declaring every function twice rather than have that PARAM macro in the public header. I much prefer to put macro hijinks in the hidden C file.
// common header
#ifdef IF_MULTI
int foo_func1(dev_t* if, int a);
int foo_func2(dev_t* if, int a, int b);
int foo_func3(dev_t* if);
#else
int foo_func1(int a);
int foo_func2(int a, int b);
int foo_func3();
#endif
// your C file
#ifdef IF_MULTI
#define IF_PARM dev_t* if,
#define GET_IF() (if)
#else
dev_t global_if;
#define IF_PARM
#define GET_IF() (&global_if)
#endif
int foo_func1(IF_PARM int a)
{
GET_IF()->x = a;
return GET_IF()->status;
}
int foo_func2(IF_PARM int a, int b)
int foo_func3(IF_PARM);
Here's a solution that won't work if you have threads (or switch interfaces on re-entrance or something like that), but it is a clean interface, and it might work for you.
You could have your single instance functions using a global DEV, and have your multi interface functions set this global and call their single instance counterparts.
For example:
dev_t *DEV;
int foo_function(int x, int y)
{
/* DEV->whatever; */
return DEV->status;
}
int foo_function_multi(dev_t *IFACE, int x, int y)
{
DEV = IFACE;
return foo_function(x, y);
}
Another option is to use variadic args, and pass and fetch an extra arg (which contains the interface to use) #ifdef MULTI, but that's horrible because you lose your type safety, and would prevent passing the arg in a register which you possibly care quite a bit about on your platform. Also, all functions with variadic args must have at least one named argument, and your question is all about avoiding arguments! But anyway:
#ifndef MULTI
dev_t *DEV;
#endif
int foo(int x, int y, ...)
{
#ifdef MULTI
va_list args;
va_start(args, y);
dev_t *DEV = va_arg(args, (dev_t*));
va_end(args);
#endif
/* DEV->whatever */
return DEV->status;
}
// call from single
int quux()
{
int status = foo(23, 17);
}
// call from multi
int quux()
{
int status = foo(23, 17, &if0);
}
Personally I prefer your first solution :-)
This will work on gcc:
#ifdef TOMSAPI_SMALL
#define TOMSAPI_ARGS( dev, ...) (__VA_ARGS__)
#else // ! TOMSAPI_SMALL
#define TOMSAPI_ARGS( dev, ...) (dev, ## __VA_ARGS__)
#endif // TOMSAPI_SMALL
#ifdef TOMSAPI_SMALL
#define TOMSAPI_DECLARE_DEVP(local_dev_ptr) device_t * local_dev_ptr = &global_dev; NULL
// The trailing NULL is to make the compiler make you put a ; after calling the macro,
// but without allowing something that would mess up the declaration if you forget the ;
// You can't use the do{...}while(0) trick for a variable declaration.
#else // ! TOMSAPI_SMALL
#define TOMSAPI_DECLARE_DEVP(local_dev_ptr) device_t * local_dev_ptr = arg_dev; NULL
#endif // TOMSAPI_SMALL
and then
int tomsapi_init TOMSAPI(device_t *arg_dev, void * arg_for_illustration_purposes ) {
TOMSAPI_DECLARE_DEVP( my_dev );
my_dev->stuff = arg_for_illustration_purposes;
return 0;
}
Using this method you would have to ensure that all of your API functions used the same name for the device pointer, but all of your function definitions and declarations would look like they needed the full number of arguments. If this were not important to you you could do:
#ifdef TOMSAPI_SMALL
#define TOMSAPI_ARGS(...) (__VA_ARGS__)
#else // ! TOMSAPI_SMALL
#define TOMSAPI_ARGS(...) (device_t *dev, ## __VA_ARGS__)
#endif // TOMSAPI_SMALL
#ifdef TOMSAPI_SMALL
#define TOMSAPI_DECLARE_DEVP() device_t * dev = &global_dev; NULL
#else // ! TOMSAPI_SMALL
#define TOMSAPI_DECLARE_DEVP(local_dev_ptr) NULL
#endif // TOMSAPI_SMALL
and then
int tomsapi_init TOMSAPI(void * arg_for_illustration_purposes ) {
dev->stuff = arg_for_illustration_purposes;
return 0;
}
But this ends up looking like dev is never declared to someone reading your code.
All of that being said, you may find that on the single device small platform that using a global device struct ends up costing more than passing the pointer around due to the number of times the address of this struct will have to be reloaded. This is more likely if you API is stacked (some of your functions call other of your functions and pass them the dev pointer), uses a lot of tail recursion, and/or your platform uses registers for passing most arguments rather than the stack.
EDIT:
I just realized that there could be a problem with this method if you have api functions which take no additional arguments, even if you do use the ## operator if your compiler wants to force you to say int foo(void) for functions that take no arguments.

Resources