Code::Blocks Forums

User forums => Using Code::Blocks => Topic started by: darkwalk on May 23, 2007, 03:43:35 pm

Title: compiles fine in command prompt, but code blocks complains
Post by: darkwalk on May 23, 2007, 03:43:35 pm
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!
Title: Re: compiles fine in command prompt, but code blocks complains
Post by: killerbot on May 23, 2007, 03:53:55 pm
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 ??
Title: Re: compiles fine in command prompt, but code blocks complains
Post by: 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:

--------- Main.cpp ------------
Code
#include <iostream>
#include "printStuff.h"
using namespace std;
int main(){
    print_hello();
    return 0;
}

--------- PrintStuff.h -----------
Code
void print_hello();

--------- PrintStuff.cpp ----------
Code
#include <iostream>
using namespace std;
void print_hello(void){
    cout<<"hello?"<<endl;
}

Thanks!
Title: Re: compiles fine in command prompt, but code blocks complains
Post by: MortenMacFly on May 23, 2007, 04:07:39 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.
Title: Re: compiles fine in command prompt, but code blocks complains
Post by: Auria on May 23, 2007, 04:13:17 pm
Also if you get more errors can you post the exact error message and not some reformulation of your own?

thanks
Title: Re: compiles fine in command prompt, but code blocks complains
Post by: killerbot on May 23, 2007, 04:27:04 pm
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 ??
Title: Re: compiles fine in command prompt, but code blocks complains
Post by: darkwalk on May 23, 2007, 04:30:27 pm
<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.
Title: Re: compiles fine in command prompt, but code blocks complains
Post by: killerbot on May 23, 2007, 04:40:37 pm
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 ;-)
Title: Re: compiles fine in command prompt, but code blocks complains
Post by: darkwalk on May 23, 2007, 04:52:08 pm
<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.
Title: Re: compiles fine in command prompt, but code blocks complains
Post by: Biplab on May 23, 2007, 04:52:29 pm
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
Title: Re: compiles fine in command prompt, but code blocks complains
Post by: killerbot on May 23, 2007, 04:59:03 pm
ok it fails for me too. Normal: you didn't add the printStuff.cpp and .h to the project !!!
Title: Re: compiles fine in command prompt, but code blocks complains
Post by: darkwalk on May 23, 2007, 05:04:14 pm
<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?
Title: Re: compiles fine in command prompt, but code blocks complains
Post by: Biplab on May 23, 2007, 05:08:05 pm
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
Title: Re: compiles fine in command prompt, but code blocks complains
Post by: darkwalk on May 23, 2007, 05:15:17 pm
<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?
Title: Re: compiles fine in command prompt, but code blocks complains
Post by: darkwalk on May 23, 2007, 05:17:07 pm
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?
Title: Re: compiles fine in command prompt, but code blocks complains
Post by: Biplab on May 23, 2007, 05:22:14 pm
While adding files, please check the targets. If a file is not added to a target, it will not compile.
Title: Re: compiles fine in command prompt, but code blocks complains
Post by: darkwalk on May 23, 2007, 05:27:02 pm
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? 
Title: Re: compiles fine in command prompt, but code blocks complains
Post by: killerbot on May 23, 2007, 05:32:28 pm
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.


Title: Re: compiles fine in command prompt, but code blocks complains
Post by: darkwalk on May 23, 2007, 05:32:36 pm
I just want to thank everyone who helped:

Biplab
killerbot
MortenMacFly
Title: Re: compiles fine in command prompt, but code blocks complains
Post by: 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!
Title: Re: compiles fine in command prompt, but code blocks complains
Post by: killerbot on May 23, 2007, 05:44:18 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 !!!
Title: Re: compiles fine in command prompt, but code blocks complains
Post by: darkwalk on May 23, 2007, 05:51:13 pm
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.
Title: Re: compiles fine in command prompt, but code blocks complains
Post by: killerbot on May 23, 2007, 05:55:10 pm
perfect.

ENJOY CODE::BLOCKS !!!!!