i have written a bison, flex and c++ files.
Compilation of bisoon and flex was passed. But when i'm trying to compile the c and cpp files:
g++ *.cpp *.c, i get strange error:
lex.yy.c:479: error: expected `;' before "static"
and when i opened lex.yy.c file, i see code that was automatically created by flex. This is some part of code around line 479, line 479 is bold:
using namespace std;
using namespace output
#line 465 "lex.yy.c"
/* Macros after this point can all be overridden by user definitions in
* section 1.
*/
#ifndef YY_SKIP_YYWRAP
#ifdef __cplusplus
extern "C" int yywrap YY_PROTO(( void ));
#else
extern int yywrap YY_PROTO(( void ));
#endif
#endif
#ifndef YY_NO_UNPUT
static void yyunput YY_PROTO(( int c, char *buf_ptr ));
#endif
#ifndef yytext_ptr
static void yy_flex_strncpy YY_PROTO(( char *, yyconst char *, int ));
#endif
i don't have any idea what to do. Please help me.
thanks
You should`t try to fix the error in file generated by lex, instead of it you need to check your lex specification for errors.
UPDATE:
Possible place to look for source of such mistake is lex specification user code section. As it is noticed in comments, LEX isn`t checking code that it generates from the specification.
Related
This question already has answers here:
C++ Global variable declaration
(5 answers)
Closed 9 months ago.
I am working on a project on Arduino ESP32 and I have a lot of Global variables (for data generation). I have decided to create a library in order to orgenise my work a little better. But I use this library into other librari's that I had to create for other usage. after compilation it I have the following error :
sketch\OX2inj_LEVEL_OX2.cpp.o:(.data.addrChipId+0x0): multiple definition of `addrChipId'
sketch\First_Useage.cpp.o:(.data.addrChipId+0x0): first defined here
sketch\OX2inj_LEVEL_OX2.cpp.o:(.bss.ChipID+0x0): multiple definition of `ChipID'
sketch\First_Useage.cpp.o:(.bss.ChipID+0x0): first defined here
here is my .ino (main) code :
#include <Arduino.h>
#include "Var_Str_EEPROM.h"
#include "Def_Global_Var.h"
#include "First_Useage.h"
//---------somthing
void setup()
{
Serial.begin(115200);
//---------somthing
Serial.println(ChipID.ReadStrEEPROM());
//---------somthing
}
void loop()
{
//---------somthing
}
here is my "Def_Global_Var.h" code
#ifndef Def_Global_Var_H
#define Def_Global_Var_H
#include "Var_Str_EEPROM.h"
uint16_t addrChipId = 1;
VarStrEEPROM ChipID(addrChipId);
#endif
here is my "First_Useage.h" code
#ifndef First_Usage_H
#define First_Usage_H
void getchipid();
#endif
here is my "First_Useage.cpp" code :
#include "First_Useage.h"
#include <Arduino.h>
#include "Var_Str_EEPROM.h"
#include "Def_Global_Var.h"
void getchipid()
{
uint32_t chipId = 0;
for(int i=0; i<17; i=i+8)
chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
ChipID.WriteStrEEPROM(String(chipId));
}
My understanding is that, when I use the #include "Def_Global_Var.h", the programme thinks that : "I am calling the library" and it sees that it has been called before and it does not like it.
Is it somehow correct ? and if it is(or not) correct what should I do?
EDIT : sorry I have put the wrong part of the prog. it has been corrected now
The actual cause is that the header is included into several source files, so you end up with multiple conflicting definitions of these variables in your .o files.
You shouldn't normally define global variables in header files at all; you should only declare them as extern:
#ifndef Def_Global_Var_H
#define Def_Global_Var_H
...
extern uint16_t addrChipId;
...
#endif
The second step is to define the variable in the corresponding .cpp file, this time without the extern keyword:
// Def_Global_Var.cpp
uint16_t addrChipId = 1;
Since Def_Global_Var.o gets linked only once, there should be no more conflicts.
My generated example.java has no code from the interface specified in the header file.
example.i:
%module example
%include "std_string.i"
%{
#include "storagepoc/storagepoc.h"
%};
using namespace std;
typedef std::string String;
%include "storagepoc.h";
storagepoc.h:
#ifndef STORAGEPOC_H
#define STORAGEPOC_H
#include <string>
class Aws::S3::S3Client;
namespace storagepoc {
class StoragePOC {
public:
/* Constructor */
StoragePOC(const std::string &options);
/* Destructor */
~StoragePOC();
/* Copy image */
std::string CopyImage(const std::string &input_url, const std::string
&storage_url, const std::string &options);
private:
std::shared_ptr<Aws::S3::S3Client> m_client;
};
}
#endif
CMakeLists.txt:
SET(CMAKE_SWIG_FLAGS -package com.intel.proxy)
swig_add_library(example LANGUAGE java TYPE USE_BUILD_SHARED_LIBS SOURCES
example.i)
SWIG_LINK_LIBRARIES(example storagepoc)
# For convenience we copy the dynamic library to the current build folder
add_custom_command(
TARGET example
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:example>
${CMAKE_CURRENT_BINARY_DIR}
)
# Build the Java code into ExampleJNI.jar
add_jar(
ExampleJNI
SOURCES
VERSION 0.0.1
${CMAKE_SWIG_OUTDIR}/example.java
${CMAKE_SWIG_OUTDIR}/exampleJNI.java
)
add_dependencies( ExampleJNI example )
#link ExampleJNI with its dependencies
#target_link_libraries(ExampleJNI example)
# Finally build the demonstration code into Main.jar
set(CMAKE_JAVA_JAR_ENTRY_POINT main)
add_jar(
Main
SOURCES main.java
VERSION 0.0.1
ENTRY_POINT main
)
add_dependencies( Main ExampleJNI )
The generated example.java is empty ???
The exampleJNI.java has the generated interface methods - StoragePOC_CopyImage...
My earlier example.java was using a
%inline %{
namespace jni {
extern int foo();
}
%}
and this generated an example.java wrapper file ??
TIA
Had to add the following to my example.i:
%pragma(java) modulecode=%{
public static String CopyImage(String input_url, String storage_url, String options) {
return new StoragePOC(options).CopyImage(input_url, storage_url, options);
}
%}
Section 21.4.2.1 http://www.swig.org/Doc1.3/Java.htm
When I used the x264 DLL in another project, the "undefined reference error" is reported when making that project!
This is my (example_exe.cpp) code:
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#define X264_API_IMPORTS
#include "x264.h"
int main(void)
{
x264_param_t *t;
x264_encoder_open(t);
return 0;
}
and This is how I compile and make my code:
g++ -c example_exe.cpp
g++ -o example_exe.exe example_exe.o -L. -llibx264-142
and I got the following error:
example_exe.o:example_exe.cpp:(.text+0x22): undefined reference to `x264_encoder_open_142(x264_param_t*)`
As you compile C++ (and not C) than you need to use extern "C" {...} for x264.h header i.e.
extern "C" {
#include "x264.h"
}
I am aiming at calling a dll file from a fortran compiler. I am doing all this in windows using the mingw compiler.
The dll was created using a g++ compiler
The code for which I am trying to create a dll
// example_dll.cpp
#include <stdio.h>
#include "example_dll.h"
__stdcall void hello()
{
printf("Hello");
}
command entered in the command prompt
g++ -c -DBUILDING_EXAMPLE_DLL example_dll.cpp
g++ -shared -o example_dll.dll example_dll.o -Wl,--out-implib,libexample_dll.a
The above two commands creates the dll file.
The job now is to create a fortran script to compile the dll file previously created.
For this purpose I am looking forward to create a fortran file capable of linking to the dll previously created.
Any help will be appreciated.
Thanks,
Adarsh
Later I tried some of the possibilities. My updated files are as follows
C file for which the DLL is created is as follows
// example_dll.c
#include <stdio.h>
#include "example_dll.h"
EXPORT void tstfunc (void)
{
printf("Hello\n");
}
EXPORT int Double11(int x)
{
printf("From the Double11\n");
printf("%d\n",x);
return x;
}
.h file used to create the DLL is as follows
// example_dll.h
#ifdef EXAMPLE_DLL_H
// the dll exports
#define EXPORT __declspec(dllexport)
#else
// the exe imports
#define EXPORT __declspec(dllimport)
#endif
// function to be imported/exported
EXPORT void tstfunc (void);
EXPORT int Double11(int x);
Fortran file used to link the dll is as follows
! fortcall.f90
program ForCall
IMPLICIT NONE
integer :: sum
integer :: inte3
INTERFACE
SUBROUTINE write() BIND(C,NAME='tstfunc')
END SUBROUTINE write
END INTERFACE
INTERFACE
SUBROUTINE get_integer(inte,inte2) BIND(C,NAME='Double11')
USE ISO_C_BINDING
IMPLICIT NONE
INTEGER (C_INT), VALUE :: inte
INTEGER (C_INT), INTENT(OUT) :: inte2
END SUBROUTINE get_integer
END INTERFACE
CALL write
CALL get_integer(1,inte3)
print*,"the output is",inte3
END PROGRAM ForCall
After entering the following directive in the command prompt
gfortran -o dll_foo_test fortcall.f90 -L. example_dll.dll
The output will be as follows
Hello
From the Double11
1
the output is -2
At this point something is not right. The code is capable of passing a value from FORTRAN to the DLL, whereas the code is not returning the right value from the dll. Some junk value of -2 is being displayed instead of 1.
I would like to fix that part in the code.
I'm building a zipper application, but it has a declaration that I want to separate it in another file (compress-file.m), but only when I separate the files I got an error when compiling with a variable, see it:
[ubuntu#eeepc:~/Desktop] make
This is gnustep-make 2.0.2. Type 'make print-gnustep-make-help' for help.
Making all for app LeafZip...
Creating LeafZip.app/....
Compiling file main.m ...
main.m: In function ‘main’:
main.m:7: error: ‘PATH_MAX’ undeclared (first use in this function)
main.m:7: error: (Each undeclared identifier is reported only once
main.m:7: error: for each function it appears in.)
main.m:12: warning: implicit declaration of function ‘compressFile’
main.m:7: warning: unused variable ‘outFileName’
make[1]: *** [obj/main.o] Error 1
make: *** [LeafZip.all.app.variables] Error 2
Also see the line 7 of main.m file:
char outFileName[PATH_MAX] = { 0 };
And see some lines of compress-file.m:
#include <stdio.h>
#include <zlib.h>
#include <limits.h>
/* Buffer to hold data read */
char buf[BUFSIZ] = { 0 };
size_t bytes_read = 0;
gzFile *out = gzopen(outFileName, "wb");
I know that is Objective-C extension, but it's only because when I solve this problem I will need to continue the development in Objective-C. What I need to do to correct this?
PATH_MAX is not always defined by including <limits.h>. If you want to use it, you probably need to fall back on the fragment:
#include <limits.h>
#ifndef PATH_MAX
#define PATH_MAX _POSIX_PATH_MAX /* Or possibly _XOPEN_PATH_MAX */
#endif /* PATH_MAX */
Did you even include limits.h in your main program? If not, you need to do so.
Looks like main.m needs to #include <limits.h>. It also seems like it will need to include a header describing compressFile (which I guess you moved into compress-file.m.