I am having this strange problem. I have the following files: main.cpp printStuff.cpp printStuff.h. The printStuff contains a single method that prints to cout. Everything looks correct, because I can compile it using the command prompt by executing:
g++ main.cpp printStuff.cpp -o output.exe
However, CodeBlocks complains because main contains an undefined reference to a function defined in printStuff.cpp. I cannot find a solution anywhere... I even installed a new nightly build because I was desperate. The only thing I can think of is due to some setting problems. Can anyone help? Thank you very much!
so it's the linking step that seems to fail
Which function is it complaining about, that is to be used in main, and as such where is it in the other file ??
Here is the code: It is the simplest kind, but doesn't work properly in CodeBlocks:
--------- Main.cpp ------------
#include <iostream>
#include "printStuff.h"
using namespace std;
int main(){
print_hello();
return 0;
}
--------- PrintStuff.h -----------
void print_hello();
--------- PrintStuff.cpp ----------
#include <iostream>
using namespace std;
void print_hello(void){
cout<<"hello?"<<endl;
}
Thanks!
Quote from: darkwalk on May 23, 2007, 03:59:28 PM
Here is the code: It is the simplest kind, but doesn't work properly in CodeBlocks:
Come on... don't you think if this wouldn't work C::B would exist?! ;-)
Please: Create a project, add all 3 files to the project, press compile. Works fine for me. Notice: The command line you stated is not the way C::B works. Within the command line you do compiling/linking in "1 step" by providing both source files. Whereas C::B will first create all object files from the sources and link them afterwards. If you try to only compile main.cpp within C::B without a project then the error message is right: C::B was not told to include the object file for PrintStuff.cpp, too - and (in fact) it wasn't even compiled!
With regards, Morten.
Also if you get more errors can you post the exact error message and not some reformulation of your own?
thanks
note that in the header and cpp you don't put 100% the same : the void param, remove it from the header file. Always best to have 100% match.
Still have problems ??
<Please: Create a project, add all 3 files to the project, press compile. Works fine for me>
Unfortunately, that does not work for me... I know this is the simplest code ever, and it should work. It must work. However, my installation does not compile.
These are the steps I took to verify my new installation of code blocks:
1. downloaded latest nightly build
2. downloaded wxmsw28u_gcc_cb.dll
3. deleted previous version and unzipped everything
4. made sure "toolchain executables" settings point to the correct compilers, linkers, debugger, etc...
I am using MingW btw.
<Also if you get more errors can you post the exact error message and not some reformulation of your own?>
The error message is:
undefined reference to 'print_hello()'
here is a screen shot of it not working:
http://peter-shih.com/images/sd.JPG
This is very strange.
could you attach your entire project (cbp file and sources)
by the way please take a screenshot of your compiler toolchain tab in compiler and debugger settings for gcc compiler
Extra remark : you main.cpp doesn't need iostream and the using namespace std ;-)
<could you attach your entire project (cbp file and sources)>
http://peter-shih.com/temp/asdf2.7z
<screenshot of your compiler toolchain tab in compiler and debugger settings for gcc compiler>
http://peter-shih.com/temp/tool_chain_sd.JPG
http://peter-shih.com/temp/compiler_settings_sd.JPG
Note: nothing is in the "other options" and "#defines" tab in compiler settings.
Quote-------------- Build: Debug in Test ---------------
mingw32-g++.exe -Wall -fexceptions -g -IC:\MinGW\include -c C:\Projects\Test\main.cpp -o obj\Debug\main.o
mingw32-g++.exe -Wall -fexceptions -g -IC:\MinGW\include -c C:\Projects\Test\PrintStuff.cpp -o obj\Debug\PrintStuff.o
mingw32-g++.exe -LC:\MinGW\lib -o bin\Debug\Test.exe obj\Debug\main.o obj\Debug\PrintStuff.o
Process terminated with status 0 (0 minutes, 1 seconds)
0 errors, 0 warnings
ok it fails for me too. Normal: you didn't add the printStuff.cpp and .h to the project !!!
<you didn't add the printStuff.cpp and .h to the project !!!>
I think I did:
http://peter-shih.com/images/sd.JPG
closed/open CodeBlocks again. Same thing. Is there a command or something to make code blocks print an inheritance list?
In the project file, you've provided, you didn't add the printstuff.h/printstuff.cpp files. I added them and it compiled fine. See the log.
Quote-------------- Build: Debug in asdf2 ---------------
mingw32-g++.exe -Wall -fexceptions -g -IC:\MinGW\include -c C:\Temp\asdf2\printStuff.cpp -o obj\Debug\printStuff.o
mingw32-g++.exe -LC:\MinGW\lib -o bin\Debug\asdf2.exe obj\Debug\printStuff.o obj\Debug\main.o
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 0 warnings
<add the printstuff.h/printstuff.cpp files>
Okay, I'm giong to sound like a noob here. I know I have to add the files in the project so that CodeBlocks can generate the necessary makefile. I think I've done so
http://peter-shih.com/images/sd.JPG
Looking at the screen shot, the "printStuff.cpp" and "printStuff.h" files are listed under the sources and headers project tree. Is this not sufficient ot include them in the project? If so, what else do I need to do?
Looking at the log:
-------------- Build: Debug in asdf2 ---------------
Linking console executable: bin\Debug\asdf2.exe
obj\Debug\main.o: In function `main':
C:/Documents and Settings/x/Desktop/asdf2/main.cpp:5: undefined reference to `print_hello()'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 0 seconds)
1 errors, 0 warnings
This verifies that "printStuff.cpp" is not compiled before the linking. How do I include "printStuff.cpp" in the compilation?
While adding files, please check the targets. If a file is not added to a target, it will not compile.
I knew it was something stupid that I'm doing. I checked the "Release" checkbox and not the "Debug" checkbox while importing a file. I reimported everything, checking all of the check boxes and it worked. I don't remember having those options on the previous version of code blocks. Is it new?
indeed when you add sources to the project, you have to select the targets you want them to belong to !!
Open up your project file (cbp with an editor),have a look at the sources part in the bottom. This is what was in your original cbp file :
Quote
<Unit filename="main.cpp" />
How does it look for yours.
In the Unit tag the targets to which it belongs to can be explicitly specified, if like in this exmaple for main.cpp nothing is specified it means it belongs to all the targets.
I just want to thank everyone who helped:
Biplab
killerbot
MortenMacFly
<How does it look for yours.>
yea, it looks correct now:
</Compiler>
<Unit filename="main.cpp" />
<Unit filename="printStuff.cpp">
<Option target="Release" />
</Unit>
<Unit filename="printStuff.h">
<Option target="Release" />
</Unit>
All three files are included. Thanks!
Quote from: darkwalk on May 23, 2007, 05:35:24 PM
<How does it look for yours.>
yea, it looks correct now:
</Compiler>
<Unit filename="main.cpp" />
<Unit filename="printStuff.cpp">
<Option target="Release" />
</Unit>
<Unit filename="printStuff.h">
<Option target="Release" />
</Unit>
All three files are included. Thanks!
well if it looks like you just posted, they are not part of the Debug target !!!
I think I didn't save the project before editing the cbp file. Does this look correct now:
<Compiler>
<Add option="-Wall" />
<Add option="-fexceptions" />
</Compiler>
<Unit filename="main.cpp" />
<Unit filename="printStuff.cpp" />
<Unit filename="printStuff.h" />
<Extensions>
<code_completion />
</Extensions>
I created a new project and imported all the files using the "debug" and "release" check box.
perfect.
ENJOY CODE::BLOCKS !!!!!