10 Replies - 1722 Views - Last Post: 12 May 2013 - 02:41 PM Rate Topic: -----

#1 mralex94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 08-May 13

Undefined reference to function, even though it is?

Posted 12 May 2013 - 12:01 PM

It's me again, this time I've got an error that's driving me insane because it should be really easy to fix, yet I can't manage it!

So I'm trying to modularise my code to make it neater and easier to read.

The error:

Quote

obj\Debug\main.o||In function `SDL_main':|
C:\Users\Alex\Documents\Assignment\Project1\main.c|46|undefined reference to `getInput'|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 0 seconds) ===|


The code:

main.c:

extern void getInput(void); //After the includes

getInput(); //This is in the main() function



input.c:

void getInput(SDL_Event event)
{
//The code is in here for keyboard input
}


Note: If I change getInput(); to getInput(event); I get an error that says too many arguments for the function?

All the files are defined properly in the project (using Code::Blocks) and are being linked too.

Thanks again!

Is This A Good Question/Topic? 0
  • +

Replies To: Undefined reference to function, even though it is?

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Undefined reference to function, even though it is?

Posted 12 May 2013 - 12:24 PM

Your extern must match the function exactly otherwise you're going to have problems.

Before you had different files did you use mismatching declarations and usage?
Was This Post Helpful? 1
  • +
  • -

#3 mralex94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 08-May 13

Re: Undefined reference to function, even though it is?

Posted 12 May 2013 - 01:05 PM

View PostKYA, on 12 May 2013 - 12:24 PM, said:

Your extern must match the function exactly otherwise you're going to have problems.

Before you had different files did you use mismatching declarations and usage?


I believe I had the correct declarations. So I've done the following:

extern void getInput(void);


Became

extern void getInput(SDL_Event event);


Which gave the error:

Quote

C:\Users\Alex\Documents\Assignment\Project1\main.c||In function 'SDL_main':|
C:\Users\Alex\Documents\Assignment\Project1\main.c|51|error: too few arguments to function 'getInput'|
C:\Users\Alex\Documents\Assignment\Project1\main.c|3|note: declared here|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 0 seconds) ===|


Which I changed:

getInput();


to:

getInput(event);


Which brings me back to the start with this error:

obj\Debug\main.o||In function `SDL_main':|
C:\Users\Alex\Documents\Assignment\Project1\main.c|51|undefined reference to `getInput'|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 0 seconds) ===|



:/ Any insight as to why this happens?
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Undefined reference to function, even though it is?

Posted 12 May 2013 - 01:13 PM

Going to have to see some context. The extern must match whatever is defined and must be called with the appropriate number/type of arguments.
Was This Post Helpful? 1
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3574
  • View blog
  • Posts: 11,114
  • Joined: 05-May 12

Re: Undefined reference to function, even though it is?

Posted 12 May 2013 - 01:20 PM

Are you linking in input.c's object file? Did you add input.c to your project or makefile?
Was This Post Helpful? 1
  • +
  • -

#6 Zereo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 108
  • Joined: 08-November 12

Re: Undefined reference to function, even though it is?

Posted 12 May 2013 - 01:24 PM

Look at the function declaration and the definition. Do you see anything that is different about them? Specifically the parameters?

This post has been edited by Zereo: 12 May 2013 - 01:27 PM

Was This Post Helpful? 1
  • +
  • -

#7 mralex94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 08-May 13

Re: Undefined reference to function, even though it is?

Posted 12 May 2013 - 01:34 PM

Since I don't want to make this page ridiculously full of code I've used pastebin. Either way there isn't that much code;

main.c:
http://pastebin.com/8QDNyq4v
main.h:
http://pastebin.com/1AUwGVvT
input.c:
http://pastebin.com/Mn3UThZC
input.h:
http://pastebin.com/FhNwf1ta
defs.h:
http://pastebin.com/ZWzumxs4

Sorry if the code is messy, I'm just trying to get everything to work first :P

View PostKYA, on 12 May 2013 - 01:13 PM, said:

Going to have to see some context. The extern must match whatever is defined and must be called with the appropriate number/type of arguments.



View PostSkydiver, on 12 May 2013 - 01:20 PM, said:

Are you linking in input.c's object file? Did you add input.c to your project or makefile?


Yes I have, and I've triple checked just to make sure.


View PostZereo, on 12 May 2013 - 01:24 PM, said:

Look at the function declaration and the definition. Do you see anything that is different about them? Specifically the parameters?


I don't think so, I've tried the same parameters, different ones, generic etc but even still it doesn't seem to work :dontgetit:
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3574
  • View blog
  • Posts: 11,114
  • Joined: 05-May 12

Re: Undefined reference to function, even though it is?

Posted 12 May 2013 - 01:40 PM

I'm not seeing your makefile or project file in that set of links.
Was This Post Helpful? 1
  • +
  • -

#9 mralex94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 08-May 13

Re: Undefined reference to function, even though it is?

Posted 12 May 2013 - 01:49 PM

View PostSkydiver, on 12 May 2013 - 01:40 PM, said:

I'm not seeing your makefile or project file in that set of links.


CodeBlocks generates its own makefile I think, but it's not possible to access directly. I've attached the project file.

Attached File  Project1.zip (743bytes)
Number of downloads: 17
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3574
  • View blog
  • Posts: 11,114
  • Joined: 05-May 12

Re: Undefined reference to function, even though it is?

Posted 12 May 2013 - 02:11 PM

You aren't compiling or linking in input.c. Additionally, even if you were compiling and linking it, you are compiling it as C++ rather than C.

Inside your .CBP file:
		<Unit filename="input.c">
			<Option compilerVar="CPP" />
			<Option compile="0" />
			<Option link="0" />
		</Unit>


Was This Post Helpful? 1
  • +
  • -

#11 mralex94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 08-May 13

Re: Undefined reference to function, even though it is?

Posted 12 May 2013 - 02:41 PM

View PostSkydiver, on 12 May 2013 - 02:11 PM, said:

You aren't compiling or linking in input.c. Additionally, even if you were compiling and linking it, you are compiling it as C++ rather than C.

Inside your .CBP file:
		<Unit filename="input.c">
			<Option compilerVar="CPP" />
			<Option compile="0" />
			<Option link="0" />
		</Unit>



That was strange, I always assumed that CodeBlocks automatically linked the files (since it asks you during the creation of the file). Must've been my fault!

Thanks for the help, the way I was checking if it was linked was wrong, :mellow:

I feel like such an idiot, thanks again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1