Expected initializer before 'token' - c++

I know there are several questions about this error message but I did not find a suitable solution for my case. I want to export the Filter class from a library. Visual Studio 2013 just compiles fine, but gcc throws the error:
prog.cpp:16:17: error: expected initializer before 'Filter'
class DllExport Filter{
^
prog.cpp:22:6: error: 'Filter' has not been declared
void Filter::setFilter(const std::vector<float>& vFilter, unsigned int uNumThreads) {
^
The code:
#ifndef _GNULINUX
#define DllExport __declspec(dllexport)
#else
#define DllExport __attribute__((visibility("default")))
#endif
#include <string>
#include <vector>
#include <iostream>
#ifdef __cplusplus
extern "C" {
namespace FilterAPI {
#endif
class DllExport Filter{
public:
static void setFilter(const std::vector<float>& vFilter, unsigned int uNumThreads);
};
void Filter::setFilter(const std::vector<float>& vFilter, unsigned int uNumThreads) {
}
#ifdef __cplusplus
} // namespace FilterAPI
} // extern "C" {
#endif
See also
https://ideone.com/3VV4AH
Edit:
flags in the make file are:
# CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 2.8
# compile CXX with /usr/bin/c++
CXX_FLAGS = -fPIC
CXX_DEFINES = -DFilter_Library_EXPORTS

I think that your problem in wrong define,
should be #ifndef __linux, you can find out gcc defaults
with gcc -dM -E - < /dev/null

I don't think you need to explicitly declare the visibility to be "default". It matters on Windows, yes, but on Linux/POSIX the default is what you want.
#ifdef _WIN32
#define DllExport __declspec(dllexport)
#else
#define DllExport
#endif
This is the more common idiom, at least.

Related

How to use CGO for header file with conditionally inline function?

I´m currently writing a Go wrapper for C api which contains header with this ifdef:
#ifdef __cplusplus
#define TEST_INLINE inline
#else
#define TEST_INLINE
#endif
TEST_INLINE int callC_inline (){
return 1;
}
Unfortunately, I cannot change header since it's a third-party code. The code compiles fine if I pass -Wl,--allow-multiple-definition to linker, but I think it's a bad practice. So, I'm interested is there any flag I can pass to CGO or trick to satisfy #ifdef __cplusplus condition?
Compilation exception:
C:\Temp\go-build318595762\cgo_issue\_obj\lib.o: In function `callC_inline':
./lib.h:11: multiple definition of `callC_inline'
C:\Temp\go-build318595762\cgo_issue\_obj\main.cgo2.o:D:/work/go/cgo_issue/lib.h:11: first defined here
collect2.exe: error: ld returned 1 exit status
main.go:
package main
//#cgo CFLAGS: -std=gnu99
//#include "lib.h"
import "C"
import "fmt"
func main() {
fmt.Printf("Go call\n")
C.callC()
}
lib.h:
void callC();
#ifdef __cplusplus
#define TEST_INLINE inline
#else
#define TEST_INLINE
#endif
TEST_INLINE int callC_inline (){
return 1;
}
lib.c:
#include "lib.h"
#include <stdio.h>
void callC(){
printf("C call\n");
}
It is possible to pass some additional info to cgo via special comments. In this case // #cgo CFLAGS: -D__cplusplus
More info here

Can't export a C++ dll with visual studio

I'm trying to create a C++ dll. I have followed the msdn tutorial but I can't compile my dll correctly.
The problem is that any function is exported. I have tested it with dumpbin.exe tool and nm tool.
In both cases, there is no detected symbols.
Here is the code of this library:
Header file:
#ifndef NLIB_H
#define NLIB_H
#ifdef _WINDLL
#define NLIB_EXPORTS __declspec( dllexport )
#else
#define NLIB_EXPORTS __declspec( dllimport )
#endif
#ifdef __cplusplus
extern "C"{
#endif
NLIB_EXPORTS int fun1(int n);
#ifdef __cplusplus
}
#endif
#endif
Source code file:
#include "nlib.h"
int fun1(int n) {
return 100;
}
I have found the error. It's necessary to add NLIB_EXPORTS to the *.c file also, like this:
#include "nlib.h"
NLIB_EXPORTS int fun1(int n) {
return 100;
}

Function in DLL is incorrectly marked dllimport

I created a DLL project called Test Lib:
// main.h
#ifndef __MAIN_H__
#define __MAIN_H__
#ifdef BUILD_DLL
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif
extern "C" {
DLL_EXPORT void print();
}
#endif
// main.cpp
#include "main.h"
#include <iostream>
#define BUILD_DLL
using std::cout;
using std::endl;
extern "C" {
DLL_EXPORT void print() {
cout << "Success" << endl;
return;
}
}
Above code is from following an example I found online that I could understand. When I try to compile and/or build it, I get the following error & warning:
error: function 'void print()' definition is marked dllimport
In function 'void print()':
warning: 'void print()' redeclared without dllimport attribute: previous dllimport ignored
This is the second library I'm ever creating because I'm trying to replicate a problem in the first one, when this happened. What is wrong? I'm using Code::Blocks.
You need to define BUILD_DLL before you include the header file main.h.
#define BUILD_DLL
#include "main.h"
As it stands in your program, you declare print with __declspec(dllimport) because the header file is processed when BUILD_DLL is not defined.

Undefined reference, but linked and included

In my Eclipse C/C++ project there is this undefined reference error which doesn't go away, doesn't matter what I do. I already changed the link order, checked if all files are compiled and included, basically everything, which is recommended in the in the Internet, when facing this problem.
Here's my program:
[...] - means that there is more code, which doesn't relate.
menulib.h
[...]
void start_GUI( void );
[...]
start_GUI.c - Note this is a .c-File
#include "menulib.h"
void start_GUI( void )
{
[...]
}
coreInterface.h
#ifndef COREINTERFACE_H_
#define COREINTERFACE_H_
#include <stdint.h>
#include <stdlib.h>
#ifdef __cplusplus
/** only include the Core when compiled for C++ */
#include "Core.h"
#endif /* #ifdef CORE */
#ifdef __cplusplus
extern "C" {
#endif
void init_GUI( Core* core);
#ifdef __cplusplus
}
#endif
#endif /* COREINTERFACE_H_ */
coreInterface.cpp - Note this is a .cpp-File
#include "coreInterface.h"
#include "menulib.h"
void init_GUI( Core* core)
{
gui_core = core;
start_GUI(); // <--- **error appears here** - calls a .c file
}
Here's the error in the makefile (the auto generated one from Eclipse):
g++ -L/home/PC/the_Project/menu_GUI -o "the_Project" [...] ./menu_GUI/coreInterface.o [...] ./menu_GUI/start_GUI.o [...] -lcurses
./menu_GUI/coreInterface.o: In function `init_GUI':
/home/PC/the_Project/Default/../menu_GUI/coreInterface.cpp:23: undefined reference to `start_GUI()'
I believe this is coming from merging the .c and .cpp files. Still I couldn't solve it. Has anyone an idea?
Kind Regards
The problem is that you share the menulib.h header between C and C++ without specifying to C++ compiler that the function start_GUI is defined in C. Use extern "C" in #ifdef not only for init_GUI, but also for start_GUI.
Working example:
#include "coreInterface.h"
extern "C" {
#include "menulib.h"
}
void init_GUI( Core* core)
{
gui_core = core;
start_GUI();
}

Warning: 'void checkGlError(const char*)' used but never defined

I'm compiling a Shared library using Android NDK r6b. All classes are C++.
I have the following two classes:
Utils.hpp
#ifdef USE_OPENGL_ES_1_1
#include <GLES/gl.h>
#include <GLES/glext.h>
#else
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#endif
#include <android/log.h>
// Utility for logging:
#define LOG_TAG "ROTATEACCEL"
#define LOG(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
#ifdef __cplusplus
extern "C" {
#endif
static void checkGlError(const char* op);
#ifdef __cplusplus
}
#endif
Utils.cpp
#include "Utils.hpp"
#ifdef __cplusplus
extern "C" {
#endif
static void checkGlError(const char* op) {
for (GLint error = glGetError(); error; error
= glGetError()) {
LOGI("after %s() glError (0x%x)\n", op, error);
}
}
#ifdef __cplusplus
}
#endif
When I want to use this function in other C++ files I #include "Utils.hpp". But, in those files I get an error:
undefined reference to `checkGlError'
Why am I getting this warning?
You've made it static. It only lives in that specific translation unit therefore. The solution is to remove the static keyword.
The warning is telling you that in the header file you "promised" there would be a definition in that translation unity if one was needed, but one has not been provided and it was needed.
static void checkGlError(const char* op);
It is a static function, that means, it has internal linkage, and therefore cannot be called from another translation unit.
Remove the static keyword from it's declaration as well as from it's definition, and it would work fine.