Compiling with gcc and terminal - c

I am attempting to compile C files from http://cocoadevcentral.com/articles/000081.php that I literally copy and pasted, so I do not believe there is any type of syntax error, yet I keep getting errors. I do not know what I am doing wrong. I'm using a text editor called TextWrangler to save my write and save my files. I have Xcode downloaded along with its terminal tools so that shouldn't be an issue either.
Here are the errors:
testc4.c:4: error: expected identifier or ‘(’ before ‘)’ token
song.c:5: error: function definition declared ‘typedef’
song.c:5: error: return type is an incomplete type
song.c: In function ‘make_song’:
song.c:6: error: ‘Song’ undeclared (first use in this function)
song.c:6: error: (Each undeclared identifier is reported only once
song.c:6: error: for each function it appears in.)
song.c:6: error: expected ‘;’ before ‘newSong’
song.c:8: error: ‘newSong’ undeclared (first use in this function)
song.c:12: warning: ‘return’ with a value, in function returning void
song.c: At top level:
song.c:15: error: expected ‘)’ before ‘theSong’
Here is the command I'm using to compile my program:
gcc testc4.c song.c -o testc4
If needed I can post the files but they are copied right from the tutorial. I named my test file testc4.c instead of whatever it wanted.
Edit: (I figured you'd need to code)
testc4.c file:
#include <stdio.h>
#include "song.h"
main ()
{
Song firstSong = make_song (210, 2004);
Song secondSong = make_song (256, 1992);
Song thirdSong = { 223, 1997 };
display_song ( thirdSong );
Song fourthSong = { 199, 2003 };
}
song.c file:
#include <stdio.h>
#include "song.h"
Song make_song (int seconds, int year)
{
Song newSong;
newSong.lengthInSeconds = seconds;
newSong.yearRecorded = year;
display_song (newSong);
return newSong;
}
void display_song (Song theSong)
{
printf ("the song is %i seconds long ", theSong.lengthInSeconds);
printf ("and was made in %i\n", theSong.yearRecorded);
}
song.h file
typedef struct {
int lengthInSeconds;
int yearRecorded;
} Song;
Song make_song (int seconds, int year);
void display_song (Song theSong);
These are straight copied from the site. I typed them out first but when it didn't work I copy and pasted them.

Probably you just have to change:
main ()
to
void main ()

Related

Windows.h functions not declared - C

I'm trying to write a test program using windows.h. I have a simple main function with a function in it and doesn't work when I compile it:
#include <windows.h>
#include <stdio.h>
int main(){
COORD coords;
SetConsoleDisplayMode(GetStdHandle(STD_OUTPUT_HANDLE), CONSOLE_FULLSCREEN_MODE, &coords);
return 0;
}
But when I compile it using "mingw32-gcc main.c" i get:
main.c: In function 'main':
main.c:107:5: warning: implicit declaration of function 'SetConsoleDisplayMode' [-Wimplicit-function-declaration]
SetConsoleDisplayMode(GetStdHandle(STD_OUTPUT_HANDLE), CONSOLE_FULLSCREEN_MODE, &coords);
^~~~~~~~~~~~~~~~~~~~~
main.c:107:60: error: 'CONSOLE_FULLSCREEN_MODE' undeclared (first use in this function)
SetConsoleDisplayMode(GetStdHandle(STD_OUTPUT_HANDLE), CONSOLE_FULLSCREEN_MODE, &coords);
^~~~~~~~~~~~~~~~~~~~~~~
main.c:107:60: note: each undeclared identifier is reported only once for each function
(It says it's in line 107 because I wrote additional functions that does not need windows.h and don't cause trouble as I don't use them, also they are commented)

C Compile Error Pointer

Hey Guys I got a Compile Error in my C Project with a Function Pointer.
Thank you for your help :)
Folder Structure:
Main Folder:
enter image description here
Lib Folder:
enter image description here
Heder Folder:
Path : C-Lib/Headers
File(s) : ShowPointer.h
Code (Main.c):
#include <stdio.h>
#include "./Headers/ShowPointer.h"
int main(){
printf("Hej this is written in VIM some C code.\n");
getchar();
}
Code (ShowPointer.c):
#include <stdio.h>
#include "../Headers/ShowPointer.h"
void ExPointer(int *pPointer, int *pPointerMax){
for (int i = *pPointer; i<*pPointerMax; i++){
printf("%d. %d %p\n", i, *pPointerMax-i, pPointerMax);
}
getchar();
}
Code (ShowPointer.h):
#ifndef SHOWPOINTER_FILE
#define SHOWPOINTER_FILE
typedef void ExPointer (*)(int , int);
#endif
Compile:
I do Compile this Project whit this code:
gcc -o main main.c Lib/ShowAddress.c
Error:
Out come of the Compile error text (code):
In file included from main.c:2:0:
./Headers/ShowPointer.h:4:25: error: expected declaration specifiers or ‘...’ before ‘*’ token
typedef void ExPointer (*)(int , int);
^
In file included from Lib/ShowAddress.c:2:0:
Lib/../Headers/ShowPointer.h:4:25: error: expected declaration specifiers or ‘...’ before ‘*’ token
typedef void ExPointer (*)(int , int);
Sorry for me bad English.
You want
typedef void (*ExPointer)(int , int);
How to create a typedef for function pointers

Simple program in C for appending a string to all files in a directory

I need a little script that appends "\n exit" to the end of all files in ~/ae23054/ my code is:
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
int main (void)
{
DIR *dp;
struct dirent *ep;
const char *path_dir ="~ae23054/Giuseppe";//Inserire la directory qui
dp = opendir (path_dir);
if (dp != NULL)
{
while (ep = readdir (dp)){
printf(ep->d_name);
char nome_file[256]=ep->d_name;
FILE *fd=fopen(nome_file, a+);
fprint(fd,"\nEXIT");
fclose(fd);
}
(void) closedir (dp);
}
else
perror ("Non posso aprire la directory");
return -1;
}
But i have this error when i compile it: gcc test.c
esempio_giuseppe.c: In function ‘main’:
esempio_giuseppe.c:16: error: invalid initializer
esempio_giuseppe.c:18: error: ‘a’ undeclared (first use in this function)
esempio_giuseppe.c:18: error: (Each undeclared identifier is reported only once
esempio_giuseppe.c:18: error: for each function it appears in.)
esempio_giuseppe.c:18: error: expected expression before ‘)’ token
Thanks for help
i have this error with :
char nome_file[256];
strcpy(nome_file, ep->d_name);
ae23054#el088soh:/home/risorse/ae23054/Giuseppe> gcc esempio_giuseppe.c
esempio_giuseppe.c: In function ‘main’:
esempio_giuseppe.c:18: warning: incompatible implicit declaration of built-in function ‘strcpy’
esempio_giuseppe.c:20: error: ‘a’ undeclared (first use in this function)
esempio_giuseppe.c:20: error: (Each undeclared identifier is reported only once
esempio_giuseppe.c:20: error: for each function it appears in.)
esempio_giuseppe.c:20: error: expected expression before ‘)’ token
You need to make the second argument to fopen a string as it requires a const char*
FILE *fd=fopen(nome_file, "a+");
^ ^
^ ^
instead of
FILE *fd=fopen(nome_file, a+);
See also #AlterMann answer regarding your file name buffer.
Problem is with char nome_file[256]=ep->d_name;
You need to use strcpy() to copy strings like, strcpy(nome_file,ep->d_name); and also change FILE *fd=fopen(nome_file, a+); to FILE *fd=fopen(nome_file, "a+");

Ruby C extension compile error: expected ‘)’ before ‘event’

I am trying to extend Ruby with a C extension in order to add an event hook.
Unfortunately, I get the following error:
timber.c:7: error: expected ‘)’ before ‘event’
timber.c: In function ‘timber_init_event_hook’:
timber.c:15: error: ‘timber_trap’ undeclared (first use in this function)
timber.c:15: error: (Each undeclared identifier is reported only once
timber.c:15: error: for each function it appears in.)
timber.c: At top level:
timber.c:21: error: expected ‘)’ before ‘event’
make: *** [timber.o] Error 1
The code I have written:
#include <ruby.h>
#include </Users/paulengel/.rvm/src/ruby-1.9.2-p180/node.h> // #include <node.h> raises `error: node.h: No such file or directory`
// Declarations
static void timber_init_event_hook();
static void timber_trap(rb_event_t event, NODE *node, VALUE self, ID mid, VALUE klass);
VALUE timber_start(VALUE self);
void Init_timber();
// Definitions
static void timber_init_event_hook() {
#if defined(RB_EVENT_HOOKS_HAVE_CALLBACK_DATA) || defined(RUBY_EVENT_VM)
rb_add_event_hook(timber_trap, RUBY_EVENT_CALL | RUBY_EVENT_RETURN | RUBY_EVENT_C_CALL | RUBY_EVENT_C_RETURN, 0);
#else
rb_add_event_hook(timber_trap, RUBY_EVENT_CALL | RUBY_EVENT_RETURN | RUBY_EVENT_C_CALL | RUBY_EVENT_C_RETURN);
#endif
}
static void timber_trap(rb_event_t event, NODE *node, VALUE self, ID mid, VALUE klass) {
rb_funcall(rb_mKernel, rb_intern("puts"), 1, rb_str_new2(event));
}
VALUE timber_start(VALUE self) {
// Do something
}
void Init_timber() {
VALUE mTimber = rb_define_module("Timber");
timber_init_event_hook();
rb_define_singleton_method(mTimber, "start", timber_start, 0);
}
Can someone help me out solving this problem? Thanks in advance.
EDIT: Try naming your rb_event_t something other than event. Even though I'm almost positive it's not a C keyword, the compile error you're getting seems to be consistent with that type of problem.
EDIT EDIT: Apparently not the actual problem, but it definitely looks like the error is on those two lines.

declaring global variables in yacc

I have a few source code files, such hashtable.c and such. The main issue is that when I write my main.c as such:
#include "tokens.h"
#include <stdio.h>
void yyerror(char *errorMsg)
{
fprintf(stderr, "%s\n", errorMsg);
}
main()
{
yyparse();
hsh = createHashtable();
}
And at the top of my yacc file (parser.y), I want to declear a hash table as such:
%{
#include <stdio.h>
#include "tokens.h"
#include "ast.c"
struct hashtable *hsh;
.............................
..............................
However I am getting this error.
main.c: In function ‘main’:
main.c:24: error: ‘hsh’ undeclared (first use in this function)
main.c:24: error: (Each undeclared identifier is reported only once
main.c:24: error: for each function it appears in.)
make: *** [main.o] Error 1
I am rather naive while it comes to C programming, any assistance will be greatful
You need an extern struct hashtable* hsh; in your main.c

Resources