1

So I have an assignment which I think I'm done with. The program should be able to encrypt or decrypt a text from a file using Caesarchiffer. So I first coded the whole thing in a single .c file and later split it into two .c files and one .h file and I keep getting the undefined reference to ´functionname'.

main.c

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "func.h"

int main(){
int arrlen = 10, key = 1;
char * text1 = "text";
char * text2 = "text";
/*some code*/
encrypt(text1, arrlen, key, text2);
/*some code*/
decrypt(text1, arrlen, key, text2);
/*some code*/
}

func.h

#ifndef FUNC_H_INCLUDED
#define FUNC_H_INCLUDED

int encrypt(char *plainText, int arrLength, int key, char *cipherText);

int decrypt(char *plainText, int arrLength, int key, char *cipherText);

#endif

func.c

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int encrypt(char *plainText, int arrLength, int key, char *cipherText){
//do stuff
}
int decrypt(char *plainText, int arrLength, int key, char *cipherText){
//do stuff
}

Only two solutions I've come up with mostly through searching is that either I've made something wrong in the main where I link to the func or I need to do something with my compiler that I don't get to work.

I'm using Code:Blocks 13.12 and GCC compiler.

Again everything works fine when I have the functions and headers in the main file so my guess is that I need to do something with the compiler. If the answer is something like

gcc main.c -o main.o -c

Give me a screenshot, can't get it to work.

Code when I had all in the main.c:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int encrypt(char *plainText, int arrLength, int key, char *cipherText);
int decrypt(char *plainText, int arrLength, int key, char *cipherText);

int main(){
int arrlen = 10, key = 1;
char * text1 = "text";
char * text2 = "text";
/*some code*/
encrypt(text1, arrlen, key, text2);
/*some code*/
decrypt(text1, arrlen, key, text2);
/*some code*/
}

int encrypt(char *plainText, int arrLength, int key, char *cipherText){
//do stuff
}
int decrypt(char *plainText, int arrLength, int key, char *cipherText){
//do stuff
}
Jongware
  • 22,200
  • 8
  • 54
  • 100
Boefst
  • 15
  • 5
  • Suggest you tag with Codeblocks if you want a CB specific answer. Because the answer at the compiler level is indeed to place all the C files into the command line: `gcc func.c main.c -o my_prog` – kaylum Dec 14 '15 at 21:34
  • In C::B add those two .c files to your project – M.M Dec 14 '15 at 23:19

2 Answers2

1

First of all include func.h in func.c

The problem is that you only compile main.c so compiler doesn't know where encrypt function is defined. You need to compile func.c file too.

Use this command

gcc main.c func.c -o main.o -c

You can check this answer: https://stackoverflow.com/a/18777608/1330198

Community
  • 1
  • 1
laltin
  • 1,134
  • 12
  • 20
  • but where do I add the command? Settings -> Global Compiler Settings -> Compiler Settings -> Other options didn't work Settings -> Global Compiler Settings -> Other linker options didn't work either – Boefst Dec 14 '15 at 21:48
  • 1
    You can create a makefile. See this question: https://stackoverflow.com/questions/835352/export-a-makefile-from-codeblocks – laltin Dec 14 '15 at 21:51
1

kaylum answered perfectly when you split your project in more than a source file (the .c files) you have to compile them all into an object file (the .o files) then the compiler can merge your object file in an exec file.

the undefined reference error means that your program use something that has not been compiled.

gcc func.c main.c -o this option specify that the two source file will be compiled in the same object file so there will the reference of the function that you call in your program.