I'd like to use the yacc/bison parser for my own project.
When building the Parser with my own Makefile, everything works fine. I took the sources from http://ymorin.is-a-geek.org/projects/kconfig-frontends (just the parser that's: /<path-to-kconfig>/libs/parser.
Now when including these files into the C++ project in Eclipse, after hitting make, the compiler crashes at the .y file, because of a syntax error - wasn't able to read the .y - syntax. So I excluded the parser from the building process.
In the answer of Yacc and Lex inclusion confusion, I've read that only the .h files have to be included, to use the functions of the parser.
So what I did is:
To get access to the parser sources out of my project, I created a simple function that just prints a line on the console in yconf.y and yconf.c: void testout(char *txt) {printf(txt);}
I even created a yconf.h with the function-head void testout(char *txt);
compiled the parser with my own makefile
the main, where I'd like to call the function looks like:
#include "y.tab.h"
#include "yconf.h"
#include <stdio.h>
extern "C"{
int main(int argc, char *argv[])
{
char configIn[] = "test";
printf(configIn);
testout(configIn);
return 0;
}
}
The error
And now when compiling, an error appears: undefined reference to 'testout(char*)'. But Eclipse itself can resolve the function - when clicking that function, the yconf.h opens. And that's why I don't know where exactly the problem is.
Eclipse Settings:
In /project/properties/C/C++ Build/Settings I put the same include paths to the parser-sources for gcc and g++ and the linker. Additionally I added in this settings as "Include files" the y.tab.h and the yconf.h
If more information are needed, please ask.
I appreciate any advises about how to solve this problem. Thanks for the support
Kind Regards
Okay one really just need the y.tab.h and y.tab.c. That is created with this makefile:
lex ./lconf.l
bison -d -y ./yconf.y
My error was: I was programming in C++ and I had the extern "C" command at the wrong place, that header file needs to be declared as c format. Here's the right code:
extern "C"{
#include "y.tab.h"
}
int main(int argc, char *argv[])
{
char configIn[] = "test";
testout(configIn);
return 0;
}
(testout is a function in y.tab.h/c with a simple printf of the parameter configIn - when executing "test" was printed.)
BTW: I excluded all source files of the parser for compilation but of the y.tab.c
Related
i have created a Makefile to run C program in shell script.but i get as error fatal error: addFunc.h: No such file or directory in mainProg.c page and addFunc.c page.i tried my best to solve this problem.but i didn't get a solution.i have mentioned my code below.
mainProg.c
#include <stdio.h>
#include "/home/name/Desktop/add/addFunc.h"
int main(){
int a,b;
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
printf("sum:%d\n",add(a,b));
return 0;
}
addFunc.h
int add(int a, int b)
addFunc.c
#include "/home/name/Desktop/add/addFunc.h"
int add(int a,int b){
return (a+b);
}
Makefile
Add: mainProg.c addFunc.c
gcc -o Add mainProg.c addFunc.c -I.
You really should be cutting and pasting, because clearly the text you've pasted here is nothing like what you're actually using (you have tons of syntax errors here).
However, the problem is that the compiler can't find your header file. This can be solved in one (or both) of two ways:
First, you should use the #include <...> form only for system headers like stdio.h etc. For your personal headers, you should use the form #include "..." instead. The difference is implementation-specific but for most compilers it is that include files using <...> are never looked for in the current directory but only in system directories and directories given with -I, while include file using "..." are looked for also in the current directory.
You could also simply ask the compiler to always look in the current directory by adding the -I. option to your compile line in your makefile.
It seems the problem you are getting is occurring because you are using #include <addFunc.h>. You would use <> when you are using a system header file. From your makefile it seems you are not.
The #include statement is a little different when you use header files of your own program because it searches in the dictionary your .c file is located.
Using #include "addFunc.h" should do the trick.
For more info on the #include directive see this page
I am using code::blocks IDE which runs on GNU GCC compiler. In my project I want to play a .wav sound file in C. I tried to play a .wav sound file with a function called PlaySound. When I compiled the code code::blocks gave me an error - PlaySoundA not declared. My code is-
#include <stdio.h>
#include <windows.h>
#include <windowsx.h>
#include <mmsystem.h>
int main(int argc, char *argv[])
{
PlaySound("C:\Snakes and Ladders\snake.wav",NULL,SND_SYNC | SND_LOOP | SND_FILENAME);
return 0;
}
I checked my path twice. I read about this function on the internet and as per me I am using it in the correct way.
In Google, I read that the function exists in a file called winmm.lib. So I put a line of code after all the headers. It was-
#pragma comment (lib , "winmm.lib")
I also added the name winmm.lib to the additional dependencies of code::blocks. So now when I compile the code it gives me another error - winmm.lib not found. Can somebody please tell me how to use PlaySound correctly.
Remove the pragma comment
Double the backslashes. The backslash is an escape character
Compile with the winmm library. Using MinGW, the command would look like this:
gcc foo.c -o foo.exe -lwinmm
Go to Settings - compiler... - linker settings. on the right side in other linker option write this:-lwinmm
I have a newbie question about the C programming language. I have looked around to find the answer in similar questions but I failed to figure it out.
Assume a simple project consisting of two dirs: src and test. The source and header files are defined by src/main.c, test/foo.h and test/foo.c.
src/main.c:
#include "../test/foo.h"
int main (void) {
int a = VAR; /* works, recognizes declared macro */
some_function(a); /* doesn't work, "undefined reference" */
}
test/foo.h:
#ifndef FOO_H
#define FOO_H
void some_function(int a);
#define VAR 2;
#endif
test/foo.c (redundant but to be complete):
#include "foo.h"
#include <stdlib.h>
void some_function(int a) {
printf("%d", ++a);
}
I created the project in Eclipse and I also compile with it, I figured it wasn't a linking error since the macro gets recognized but the method is not callable.
The reason why I'm using different directories is because I have a lot of files and would like my test code to be separate from my main source code. Note that src and test have the same parent directory.
Any ideas what's going on here? Am I missing something very obvious?
Any help would be much appreciated, thanks in advance!
edit: I'm working on a (Debian) Linux machine and Eclipse uses the gcc compiler.
edit2: Thanks to H2CO3's answer I learned it is indeed a linking error. Since compiling and linking manually every time is quite an overhead, I was wondering if anyone knows how to teach Eclipse to link executables from different directories?
--------------------- SOLUTION ---------------------
edit3: Lol the solution was very easy after all, all I had to do was create a "new source folder" rather than a "new folder". I feel stupid but thanks to you all for replying, H2CO3 in particular!
I figured it wasn't a linking error since the macro gets recognized but the method is not callable.
Non sequitur. Macros are expanded in the preprocessing phase. (And as such, they have nothing to do with linkage at all.) You do have a linker error.
What you have to do is compile both files then link them together, so something like this should work:
gcc -Wall -o dir_one/foo.o dir_one/foo.c
gcc -Wall -o dir_two/bar.o dir_two/bar.c
gcc -o my_program dir_one/foo.o dir_two/bar.o
Also, read this SO question/answer and/or this article to understand how the steps of the compilation process work together. (These are almost the same for C and C++, it's only the name mangling that usually differs.)
I'm trying to set up some .c files to make it easier on me to find things, once it starts becoming larger. I'll be using SDL calls in the program, hence the includes.
Here's how my main.cpp looks right now:
#include <stdio.h>
#include <SDL.h>
#include <SDL_gfxPrimitives.h>
#include <SDL_ttf.h>
#include "WriteText.h"
int main(int argc, char *argv[])
{
int i =0;
i = b();
return 0;
}
In my WriteText.c I have:
#include "WriteText.h"
int b(void)
{
return 3;
}
Finally my WriteText.h:
#ifndef WRITETEXT_H_INCLUDED
#define WRITETEXT_H_INCLUDED
int b(void);
#endif // WRITETEXT_H_INCLUDED
Trying to compile it, I get an undefined reference to 'b()'. I have no idea why this is happening, I practiced it in some basic example codes and everythin works just fine, but as soon as I'd actually use it for something practical I hit an error like this.
The problem is that you are not linking the WriteText.c into your executable. If you gave some more information about how you are creating the executable we could probably give better help.
Chances are your compilation isn't using the writetext object file. Assuming *nix and gcc, your makefile should look something like:
all: myprog
myprog: myprog.o writetext.o
gcc -o $# $^
myprog.o: myprog.cpp
writetext.o: writetext.cpp
I figured out my issue, my main file was .cpp and used CPP compiler (because it was an SDL project), but the new file I added was .c and used C compiler, when I added a new .cpp file and just copy-pasted my code into it, everything worked smoothly.
(I'm not sure if it's "bad form" to write C code in .cpp files but if I intend to use SDL I guess that's the only way to go.)
I'm keep getting this "Unable to resolve identifier file" message on netbeans.
I'm new in c and netbeans.
It was fine last night but somehow after rebooting my computer this message keep occurs.
Here's a code. What would be the problem?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char** argv) {
char filename[] = "text.dat";
char line[5];
FILE *file = fopen(filename, "r");
return 0;
}
I know this is an old post, but I ran into the same issue today. Don't be too quick to assume a compiler-supplied header file is defective. That is rarely the case, especially for headers such as stdio.h that have been around a long time.
Keep in mind that Netbeans code assistance references the includes used within your source code. Any macros that are used by the compiler must be defined to Netbeans. A file such as stdio.h may have conditional includes based on one or more macros. Unless Netbeans is aware of those macros it cannot apply them when it processes include files to provide code assistance. This would prevent conditional headers containing symbols from being loaded.
For example, today I saw that an include file I use has many conditional includes and the symbols Netbeans reported it could not resolve were defined in those files. Knowing that I was building for a particular processor I determined the macro needed for the proper file to be included within . I then defined that macro in Project Properties/Code Assistance/C Compiler/Preprocessor Definitions. At that point Netbeans was able to resolve the symbols.