Question

I'm writing a C application using allegro and I need some C++ functionnalities so i wrote a C/C++ interface for my functions using extern "C" But it gives me a lot of warning for implicit declaration of theese functions

Here is my code in lists.h

#ifndef LISTS_HPP_INCLUDED
#define LISTS_HPP_INCLUDED

#include "entities.h"
#include "engine.h"

#ifdef __cplusplus

extern "C"
{
    int cloneList_size(void);
    int colList_size(void);
    int scene_size(void);

    void cloneList_clear(void);
    void colList_clear(void);
    void scene_clear(void);

    void push_back_cloneList(Object *object);
    void push_back_colList(t_collision *col);
    void push_back_scene(Object *object);

    void remove_cloneList(Object *object);
    void remove_scene(Object *object);

    Object* Scene(int nbr);
    Object* CloneList(int nbr);
    t_collision* ColList(int nbr);
}
#endif // __cplusplus

#endif // LISTS_HPP_INCLUDED

Here is my code in lists.cpp

#include "lists.h"

#include <iostream>
#include <vector>
#include <stdexcept>


std::vector<Object *> scene;
std::vector<t_collision *> colList;
std::vector<Object *> cloneList;

int cloneList_size(void)
{ //..
}
//And all other functions here

I can't include lists.h in my main.c or I have an error because the C compiler doesn't recognize the extern "C" part

If I don't include lists.h it works but with many implicit declaration of function warnings..

I'm using Codeblocks 13.12 and I don't know how to enable the C++ compilation option as someone suggested in another forum when I googled my problem.

What am I doing wrong and what should I do to correct these warnings..

Was it helpful?

Solution

This is not how you do the #ifdef __cplusplus/extern "C" thing. It's supposed to look like this:

#ifdef __cplusplus
extern "C" {
#endif

int cloneList_size(void);
...
t_collision* ColList(int nbr);

#ifdef __cplusplus
}
#endif

In your version, a C compiler does not see any of the function declarations because you forgot to add another #endif and another #ifdef __cplusplus around them. For a C compiler __cplusplus is not defined. But it still needs to see the function declarations -- just without the extern "C" block around them.

OTHER TIPS

Replace

int cloneList_size();

with

int cloneList_size(void);

In C++, () or (void) in a function declaration means "no parameters". In C, (void) means "no parameters" while () means "some unknown but fixed number of parameters".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top