I am a CS student currently trying to learn C. I just started creating my own header files and as a fun little project I wanted to create a library of my own that can handle big integers in a string based way. There are simply three files; bignumbers.h, bignumbers.c, test.c .
bignumbers.h:
#ifndef BIGNUMBERS_H
#define BIGNUMBERS_H
//Typedef Declaration
typedef struct big_number{
char digit;
struct big_number *next_digit;
} *BIGNUMBER;
//Function Declaration
int number_length(char number[]);
BIGNUMBER create_digit(char digit);
BIGNUMBER create_number(char number[]);
//Macro Declaration
#endif // BIGNUMBERS
bignumbers.c
#include <stdlib.h>
#include <stdio.h>
//Actual functions
#include "bignumbers.h"
int number_length(char number[]){
/*code to get string length*/
}
BIGNUMBER create_digit(char digit_){
BIGNUMBER number_digit;
number_digit = malloc(sizeof(BIGNUMBER));
if(number_digit == NULL){
printf("Memory allocation failed!");
exit(EXIT_FAILURE);
}
number_digit->digit = digit_;
}
BIGNUMBER create_number(char number[]){
}
test.c:
#include <stdio.h>
#include <stdlib.h>
#include "bignumbers.h"
int main(){
number_length("1");
}
Trying to compile test.c gives me an error with the message "undefined reference to 'number_length'
error: ld returned 1 exit status"
There is no typo in test.c as far as I'm aware. So what might be the problem here? I understand that "ld returned 1 exit status" is a linker problem but I don't really see what can cause that.
Related
I am making a game and I have to add some sounds effects and Music.
I Googled it and I found The flowing Code:
#include <conio.h>
#include "inc/fmod.h"
FSOUND_SAMPLE* handle;
int main ()
{
// init FMOD sound system
FSOUND_Init (44100, 32, 0);
// load and play mp3
handle=FSOUND_Sample_Load (0,"my.mp3",0, 0, 0);
FSOUND_PlaySound (0,handle);
// wait until the users hits a key to end the app
while (!_kbhit())
{
}
// clean up
FSOUND_Sample_Free (handle);
FSOUND_Close();
}
But when I compile it I got the flowing error:
➜ Desktop gcc main.c
main.c:1:10: fatal error: 'conio.h' file not found
#include <conio.h>
^~~~~~~~~
1 error generated.
Well, firstly <conio.h> is a C++ library and you're programming in C. It's different!
Then, I remember a C code I wrote years ago, main.c has got the following code (comments are in italian because I am italian):
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "header.h"
int main(){
register unsigned char x='2';
printf("digitare tasti:\n");
while(1){
while(1){
if(x=='2'){/*blocco2*/ while(x!='1' && x!='3'){x=getch(); scala2(x);}}
if(x=='1'){/*blocco1*/ while(x!='2' && x!='3'){x=getch(); scala1(x);}}
if(x=='3'){/*blocco3*/ while(x!='1' && x!='2'){x=getch(); scala3(x);}}
}
}
system("PAUSE");
return 0;
}
Then, this is the other source file, called file.c:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "header.h"
void scala1(unsigned char x){
if(x=='a')beep(131,50);
if(x=='s')beep(147,50);
if(x=='d')beep(165,50);
if(x=='f')beep(175,50);
if(x=='g')beep(196,50);
if(x=='h')beep(220,50);
if(x=='j')beep(247,50);
if(x=='k')beep(262,50);
if(x=='l')beep(294,50);
if(x=='w')beep(139,50);
if(x=='e')beep(156,50);
if(x=='r')beep(185,50);
if(x=='t')beep(208,50);
if(x=='y')beep(233,50);
}
void scala2(unsigned char x){
if(x=='a')beep(262,50);
if(x=='s')beep(294,50);
if(x=='d')beep(330,50);
if(x=='f')beep(349,50);
if(x=='g')beep(392,50);
if(x=='h')beep(440,50);
if(x=='j')beep(494,50);
if(x=='k')beep(523,50);
if(x=='l')beep(587,50);
if(x=='w')beep(277,50);
if(x=='e')beep(311,50);
if(x=='r')beep(370,50);
if(x=='t')beep(415,50);
if(x=='y')beep(466,50);
}
void scala3(unsigned char x){
if(x=='a')beep(523,50);
if(x=='s')beep(587,50);
if(x=='d')beep(659,50);
if(x=='f')beep(698,50);
if(x=='g')beep(784,50);
if(x=='h')beep(880,50);
if(x=='j')beep(988,50);
if(x=='k')beep(1046,50);
if(x=='l')beep(1175,50);
if(x=='w')beep(554,50);
if(x=='e')beep(622,50);
if(x=='r')beep(740,50);
if(x=='t')beep(831,50);
if(x=='y')beep(932,50);
}
The last one, the file header.h. It's code is the following one:
void scala1(unsigned char x);
void scala2(unsigned char x);
void scala3(unsigned char x);
All the source files must be in the same directory. You compile main.c and then, you just need to press a,s,d,..y and 1,2,3. Try! It works, of course if you want to change part of the code, you can do. I hope you enjoy my program, it's funny :)
While compiling the C code as follows:
#include <stdio.h> // Notice the library included in the header of this file
#include <stdlib.h>
#include "myLibrary.h" // Notice that myLibrary.h uses different include syntax
#define MAX_LENGTH 21.8
#define WORK_WEEK 5
int main(void) {
function1();
return EXIT_SUCCESS;
}
I am getting the following:
d:/programs/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\svtte\AppData\Local\Temp\ccyHWfzC.o:02_01.c:(.text+0xc): undefined reference to `function1'
collect2.exe: error: ld returned 1 exit status
The myLibrary.h file is as follows:
#ifndef MYLIBRARY_H_
#define MYLIBRARY_H_
void function1(void);
void function2(void);
#endif /* MYLIBRARY_H_ */
and myLibrary.c is as follows:
void function1(void){
puts("It works :)");
}
void function2(void){
//This function does nothing as well
}
Any reasons as to why I am getting the error response would be helpful. Also, any pointers to the possible fixes would be great.
The likelihood is that you’re not including 'myLibrary' when you link to build the executable.
This question already has answers here:
Why are typedef identifiers allowed to be declared multiple times?
(3 answers)
Closed 5 years ago.
Why don't I get the error messages?
I have three header files named DataType.h, printInt.h, printStr.h, and one myApp.c.
DataType.h
typedef int Integer;
typedef char String;
printInt.h
#include "DataType.h"
void printInt(Integer);
printInt.c
#include "printInt.h"
#include <stdio.h>
void printInt(Integer number){
printf("%d\n", number);
}
printStr.h
#include "DataType.h"
void printStr(String*);
printStr.c
#include "printStr.h"
#include <stdio.h>
void printStr(String *str){
printf("%s\n", str);
}
myApp.c
#include "printStr.h"
#include "printInt.h"
Integer main(void){
printInt(20);
printStr("hello");
return 0;
}
Clearly, I have included the DataType.h twice, and I did not use #ifndef to avoid redefinition of Integer and String. Please, someone, tell me how I can get the error messages to demonstrate the directive is working properly.
#ifndef __DATATYPE_H
#define __DATATYPE_H
typedef int Integer;
typedef char String;
#endif
Regardless having #ifndef or not, the gcc complier (version 5.4.0) does not generate any error messages. What's wrong?
Definitions of typedef and prototypes of functions can occur as many times as you want. For example:
typedef int lala;
typedef int lala;
void somePrototype();
void somePrototype();
int main() {
return 0;
}
will compile just fine: https://ideone.com/4EjfaR
Try adding the definition of a function to a header file. You will see then that you get a redefinition error and will require a header guard.
So whenever I try to run my Makefile on my server, it always gives me the error is "Memory.c: 9 error: expected ')' before '*' token. But when I try to run it on my own computer, it works just fine. I've been trying to figure out what is wrong but can't seem to find it.
I've attached the 3 files that are used in this part of my program. Memory.c, Memory.h and ProcessInput.h.
This is Memory.c
/* Initializes memory */
#include <stdio.h>
#include <stdlib.h>
#include "memory.h"
void initializeMemory(memory** memArray, int memSize)
{
// Allocating space for memory array
*memArray = malloc(memSize * sizeof(memory));
if(*memArray == NULL)
{
fprintf(stderr, "Error allocating space for array of memory" );
exit(1); // exit(1) = Unsuccessful exit
}
// Initializing the contents within memory array
int i = 0;
for(i = 0; i < memSize; i ++)
{
((*memArray)[i]).occupied = false;
}
}
and this is Memory.h
// Definitions for Memory.c
#define bool int
#define true 1
#define false 0
#include "ProcessInput.h"
// Include guards to prevent redefinition of struct
#ifndef MEMORY_H
#define MEMORY_H
typedef struct memoryDetail
{
process process;
bool occupied;
} memory;
#endif
// Function declaration for memory.c
void initializeMemory(memory** memArray, int memSize);
the only thing used from ProcessInput.h is the process structure defined in ProcessInput.h
This is ProcessInput.h
// Include guards to prevent redefinition of struct
#ifndef PROCESSDETAIL_H
#define PROCESSDETAIL_H
typedef struct processDetail
{
int timeCreated;
int processID;
int memorySize;
int jobTime;
} process;
#endif
// function declarations for ProcessInput.c
void processInput(int* maxSize, int* count, process** processes, char* fileName);
I'm not too sure why it's giving me the error. I don't know where I'm supposed to be putting a missing right brace. Any advice is much appreciated!
edit: As informed, these are the following questions that I looked at but to not avail.
error: expected ‘)’ before ‘*’ token
Multiple of same error while compiling "error: expected ')' before '*' token
http://www.dreamincode.net/forums/topic/288956-error-expected-before-token/
thanks everyone for the help!
#include "memory.h" is different to #include "Memory.h" (i.e. C is case sensitive)
If you tried #include "myfile.h" instead of #include "MyFile.h" the error may be more obvious. In this case it just happens that the compiler finds the system memory.h.
<memory.h> is a header from C library of pre-standard era. It is quite possible that your standard library still provides it and the compiler takes that one instead of yours.
Try renaming your header file and see if it changes anything.
I'm building a small file utility that relies on queues and I've been getting the error on compile:
error: request for member "entries" in something not a structure or union
I stripped it down to just the queue handling lines, and I'm getting the same error, here's the source for the header:
#ifndef _tailq_test_h
#define _tailq_test_h
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/queue.h>
struct tail_q {
TAILQ_ENTRY(tail_q) entries;
int item;
};
TAILQ_HEAD(tail_queue, tail_q);
static struct tail_queue queue;
int main();
#endif
And for the program:
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/queue.h>
#include "tailq-test.h"
static struct tail_queue queue;
int main()
{
struct tail_q *q_ptr;
int data = 1;
TAILQ_INIT(&queue);
TAILQ_INSERT_HEAD(&queue, &data, entries);
return 0;
}
The traceback refers to the line TAILQ_INSERT_HEAD(&queue, &data, entries);, it has the same effect if TAILQ_INSERT_TAIL is used instead.
I'm not sure why it's not compiling. I checked the answer from this question and the provided example compiled just fine. I'm having trouble spotting the difference/what I'm doing wrong.
Any help would be much appreciated.
I wasn't too familiar with TAILQ, so I took a look at the source code that you can find here : http://www.gnu.org/software/mifluz/doc/doxydoc/queue_8h-source.html
Here is the source code for TAILQ_INSERT_HEAD :
#define TAILQ_INSERT_HEAD(head, elm, field) do {
if (((elm)->field.tqe_next = (head)->tqh_first) != NULL)
(head)->tqh_first->field.tqe_prev =
&(elm)->field.tqe_next;
As you can see, the second parameter elm needs to be a struct containing the third parameter as a member (field).
In your situation, your second parameter is an int *, which does not have an entries member as pointed out by your error message.
From my understanding, you must pass a tail_q struct as your second parameter for your code to compile.
EDIT :
For example, the following code compiles :
int main()
{
struct tail_q q;
int data = 1;
q.item = data; // Include your data in your struct.
TAILQ_INIT(&queue);
TAILQ_INSERT_HEAD(&queue, &q, entries); // Notice that I am passing a tail_q pointer here.
return 0;
}