Jump to content
  • Advertisement
Sign in to follow this  
luxm

Split project in parts [C++]

This topic is 4230 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all.. Im trying to split some functions of my main project in different files. What i did: (note its just a test project(!) ) main.c #include <cstdlib> #include <iostream> #include "x.h" // my file (!) int main() { cout << calc(1,1); system("PAUSE"); return EXIT_SUCCESS; } x.h #ifndef __X_H__ #define __X_H__ #endif int calc(int a,int b); and , x.c: #include "x.h" int calc(int a,int b) { return (a+b); } Compiler returns this error message(: [Linker error] undefined reference to `calc(int, int)' and : ld returned 1 exit status Why im getting this error ?

Share this post


Link to post
Share on other sites
Advertisement
x.h
#ifndef __X_H__
#define __X_H__
#endif
int calc(int a,int b);

Should be:

x.h
#ifndef __X_H__
#define __X_H__

int calc(int a,int b);

#endif


If you don't understand the #indef etc, they are 'C preprocessor macros' and here they are used as 'inclusion guards'. Google those terms. Though at first glance i don't think that should yield your error, i can't see what does otherwise, but it would still know that the function is there.

Hope that helps,

Dave

Share this post


Link to post
Share on other sites
Well i can't see anything wrong with that code, maybe it is something to do with DevC++, i use Visual Studio so i cannot help with that. Is that the exact contents of the files?

Share this post


Link to post
Share on other sites
Any particular reason you use __X_H__ as opposed to the shorter X_H.

Especially seeing that anything(*) beginning with an underscore is reserved for the compilers use only.

Its considered normal practise to name c++ source files .cpp, and c source files .c. People tend to use .h for both c and c++ header files, but some people like to use .hpp for c++ headers too.


As for your error, is x.c included for compilation? To see if it is, place the line "#error I am being included for compilation" in the file. If this halts your compilation you know that it is being compiled.

(*)Not completely true, but close enough.

Share this post


Link to post
Share on other sites
I noticed something weird!..

If i do this in the main project:

//include c & h
#include "x.c"
#include "x.h"

or :

//x.c
#include "x.c"

or , if i just add this in x.h:
#include "x.c"

The program runs fine...but , i think it isnt right to include the ".c" file...or im wrong ??

~~~~~~Rip-off
Any particular reason you use __X_H__ as opposed to the shorter X_H.
Rip-off~~~~~~

Well , i just did a search on google and i used the 1st example i found as reference.

Share this post


Link to post
Share on other sites
Baed on your last post, it seems that either:

a) the file x.c isnt being compiled (is there a file called x.o in the project directory? )

b) the file x.c is being compiled as a c file, and the linker cannot resolve the c++ mangled signature to the c unmangled signature.

I can't see how it would do this and then allow the compilation of main.c as a c++ file. I just dont know [smile]

#includeing a source file is a bad idea. You are not really seperating the project at all then, as the compiler only sees one large file rather than lots of smaller ones ( your compilation will take just as long too ).

Also, it introduces the scope for errors, duplicate definitions and all manner of unpleasantness.

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off

a) the file x.c isnt being compiled (is there a file called x.o in the project directory? )
.


There's only one .o file , which is the "main.o" (The main src)..

Share this post


Link to post
Share on other sites
Quote:
Original post by luxm
Quote:
Original post by rip-off

a) the file x.c isnt being compiled (is there a file called x.o in the project directory? )
.


There's only one .o file , which is the "main.o" (The main src)..


Ok, you need to tell your compiler to compile x.c

What compiler/IDE are you using? Most IDEs will compile all source files added to the project.

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
Quote:
Original post by luxm
Quote:
Original post by rip-off

a) the file x.c isnt being compiled (is there a file called x.o in the project directory? )
.


There's only one .o file , which is the "main.o" (The main src)..


Ok, you need to tell your compiler to compile x.c

What compiler/IDE are you using? Most IDEs will compile all source files added to the project.


Im using dev c++...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!