Including c files in .msg files (Veins) - c

I'm trying to implement a BSM in Veins 4.4. In order to reach this purpose I would like to import my libasn which are C files (.c and .h ) in the WaveShortMessage.msg in order to populate the BSM.
I tried to import them as follows:
#include "veins/asn/BasicSafetyMessage.h"
#include <veins/asn/BasicSafetyMessage.h>
3.
extern "C" {
#include "veins/asn/BasicSafetyMessage.h"
};
4.
#ifdef __cplusplus
extern "C" {
#endif
#include "veins/asn/BasicSafetyMessage.h"
#ifdef __cplusplus
}
#endif
but it doesn't work. It always returns an error.
Could you suggest the right way to do it? Thanks a lot

Using a C or C++ code in the message's definition in OMNeT++ is described in details in OMNeT++ Simulation Manual, chapter 6.5.
You should use cplusplus keyword, an example for C code in BasicSafetyMessage.h:
cplusplus {{
extern "C" {
#include "veins/asn/BasicSafetyMessage.h"
};
}}

Related

Using C++11 and C libraries together for complex numbers

I would like to use a C Library and a C++11 library in my application. It seems the usage of "complex" in the C and C++11 library conflicts, and it produces compilation error.
A MWE is given here.
Contents of myLib_C.h:
#ifndef MYLIBC_H
#define MYLIBC_H
#include <math.h>
#include <complex.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef float complex cfloat;
typedef double complex cdouble;
#define myFunc_cfloat(r,i) ((float)(r) + ((float)(i))*I)
#define myFunc_cdouble(r,i) ((double)(r) + ((double)(i))*I)
#ifdef __cplusplus
} // extern "C"
#endif
#endif
The contents of myLib_CPP.h:
#ifndef MYLIBCPP_H
#define MYLIBCPP_H
#include "myLib_C.h" //uses myLib_C somewhere in this file
#include <iostream>
#include <complex>
inline void CppFunction()
{
std::cout<<"This file need to be compiled using C++11\n";
std::complex<float> a(10,100);
std::complex<float> b(1, 1);
auto c = a+b;
std::cout<<"c= "<<c<<std::endl;
}
#endif // MYLIBCPP_H
My main.cpp:
#include "myLib_C.h"
#include "myLib_CPP.h"
#include <iostream>
#include <complex>
int main()
{
std::cout<<"Hello World\n";
CppFunction();
return 0;
}
The contents of CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
project(myTest)
set(CMAKE_CXX_FLAGS "-std=c++11")
add_executable(myTest main.cpp)
When I compile, I get the following error:
error: expected initializer before ‘cfloat’
typedef float complex cfloat;
A similar problem was discussed in C Complex Numbers in C++?. The solution mentioned there is to replace complex with _Complex. This is not possible in my case as I will not be able to edit the C and C++ libraries.
extern "C" { ... } doesn't magically turn C code inside braces into valid C++ code, so this is out.
Both language standards require their respective complex numbers to have the same layout as an array of two numbers of the corresponding floating-point type (i.e. both C++ std::complex<float> and C float complex behave just like float[2], layout-wise). You can exploit this. For example:
#ifdef __cplusplus
using cfloat = std::complex<float>;
#else
typedef float complex cfloat;
#endif
Now you can declare a cfloat variable in one language and pass it to the other language, and this should work.

Eclipse C constants defined in header could not be resolved

I'm writing a C program where I use a header to declare some functions and define several constants. When I use these constants in my code, I get the error Symbol 'FOO' could not be resolved. I have no problems with the declared functions.
I am using Eclipse Neon. I have already verified that the path to the directory containing the header files is added to the Include Path. And I have also restarted Eclipse.
header.h
#ifndef __header_h_
#define __header_h_
#define FOO 0x00
#define BAR 0x01
void do_stuff(int x);
#endif
main.c
#include <stdio.h>
#include "header.h"
int main() {
do_stuff(FOO);
return 0;
}
ERROR: Symbol 'FOO' could not be resolved
What am I missing here?
Right Click on the Project Index > Freshen All Files and then Index > Rebuild fixed the problem

Error linking my svm.cpp file using LIBSVM with my C program

I am trying to use the LIBSVM package on Eclipse IDE using C.
In the README file, it states that "You need to #include "svm.h" in your C/C++ source files and
link your program with `svm.cpp'."
I already included "svm.h" in all my source files and I added my 'svm.cpp' file under the source code folder. However, I got an error "template with C linkage" in the 'svm.cpp' file.
Does the error have anything to do with the way I added the 'svm.cpp' file in my program? I really do not understand and I tried searching everywhere.
svm.cpp:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <float.h>
#include <string.h>
#include <stdarg.h>
#include <limits.h>
#include <locale.h>
#include "libsvm.h"
#include "main.h"
#ifdef __cplusplus
extern "C" {
#endif
int libsvm_version = LIBSVM_VERSION;
typedef float Qfloat;
typedef signed char schar;
#ifndef min
template <class T> static inline T min(T x,T y) { return (x<y)?x:y; }
#endif
#ifndef max
template <class T> static inline T max(T x,T y) { return (x>y)?x:y; }
#endif
template <class T> static inline void swap(T& x, T& y) { T t=x; x=y; y=t; }
template <class S, class T> static inline void clone(T*& dst, S* src, int n)
.
.
.
.
.
.
.
.
#ifdef __cplusplus
}
#endif
What version of libsvm you are using, and where did you download it from?
This version of svm.cpp does not have extern "C" around templates.
If you added them yourself, you have no-one else to blame.
If your version came with extern "C", that version is broken: templates really can't have extern "C" linkage.
Update:
I added the extern "C" by myself because I thought it was necessary. Is it wrong?
Yes, it is.
when I did not add extern "C", there were more errors
You should accept this answer to your question, remove your modifications, and ask a new question detailing the other errors.

namespace and extern 'C' compilation errors

I have tried to google the errors but to no avail. Maybe I am searching in the wrong direction.
I am trying to compile a file but when i tired to compiled it, the compiler gave me these errors:
C2059 syntax error: ';'
C2059 syntax error: '}'
C2061 syntax error: identifier 'namespace_name'
C2449 syntax error: found '{' at file scope (missing header?)
In my code.c file:
#ifndef myclass_c
#define myclass_c
#include "classA.h"
#include "classB.h"
#include "conversionLib.h" // no namespace is being used here
namespace namespace_name {
// ... generated codes in C....
}
#endif
In classA.h and in classB.h:
#ifndef myclassA_h // myClassB_h if in classB
#define myclassA_h // myClassB_h if in classB
#include "classC.h" // both classA and classB include classC.h
namespace namespace_name {
#ifdef __cplusplus
extern "C" {
#endif
// ... generated codes in C....
#ifdef __cplusplus
}
#endif
}
#endif
and in classC.h (same for classD.h)
#ifndef myclassC_h // myclassD.h for classD.h
#define myclassC_h // myclassD.h for classD.h
#include "classD.h" // no includes in classD.h
namespace namespace_name {
#ifdef __cplusplus
extern "C" {
#endif
// ... generated codes in C....
#ifdef __cplusplus
}
#endif
}
#endif
what am i doing wrong? or have i missed out something?
Any suggestion would be greatly appreciated.
I apologized if this question/format is confusing.
Thanks in advance
The main problem seems to be, that you use c++ syntax in a .c file.
C does not support namespaces!
It does not make much sense to put a extern "C" function into an namespace. in C++ the compiler does something called 'name mangeling' it actually puts the name space (e.g. the class name, the namespace) and additional information (like function or template parameter) of an identifier into its symbol name that is the input to the linker.
extern "C" tells the C++ Compiler not to do so. so you lose the binding to the surrounding namespace (as well as the possibilities to overload the function).
It may be a nice thought to put the functions in a special namespace if they are included from C++ but on the other hand mostly in C namespaces are build y using a special prefix to the function name. so in C++ you end up with the explicit c++ name space and the implicit function prefix name space.
If you still want a namespace in c++ around your C-Functions you have to include the namespace definition into your #ifdef __cplusplus blocks. (but i do not even know if this is allowed by the C++ standard)

weird c function syntax

I know my C up to this point. I was looking at the source files of PHP I downloaded, and I saw this strange syntax:
PHPAPI int php_printf(const char *format, ...)
{
// code...
}
What does the PHPAPI do before the return type int? I've tried searching all over and I can't understand what this means. Is it a second return type? It can't be because the function does return an int. Maybe it extends to some other struct declared in a header file?
The hard way:
Go to the makefile and add in the line that compiles the sources: -E, by doing so you will see the source cose after the preprocessing phase.
The easy way:
Search all the project for PHPAPI:
find it in php.h:
#ifdef PHP_WIN32
#include "win95nt.h"
# ifdef PHP_EXPORTS
# define PHPAPI __declspec(dllexport)
# else
# define PHPAPI __declspec(dllimport)
# endif
#define PHP_DIR_SEPARATOR '\\'
#else
#define PHPAPI
#define THREAD_LS
#define PHP_DIR_SEPARATOR '/'
#endif
Now what you need to know is what is __declspec(dllexport) and what is __declspec(dllimport)
In the SO thread- What is __declspec and when do I need to use it?
see Alexander Gessler answer:
The canonical examples are __declspec(dllimport) and
__declspec(dllexport), which instruct the linker to import and
export (respectively) a symbol from or to a DLL.
// header
__declspec(dllimport) void foo();
// code - this calls foo() somewhere in a DLL
foo();
(__declspec(..) just wraps up Microsoft's specific stuff - to
achieve compatibility, one would usually wrap it away with macros)

Resources