Author Topic: compiles fine in command prompt, but code blocks complains  (Read 9146 times)

Offline darkwalk

  • Multiple posting newcomer
  • *
  • Posts: 25
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!

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5247
Re: compiles fine in command prompt, but code blocks complains
« Reply #1 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 ??

Offline darkwalk

  • Multiple posting newcomer
  • *
  • Posts: 25
Re: compiles fine in command prompt, but code blocks complains
« Reply #2 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: [Select]
#include <iostream>
#include "printStuff.h"
using namespace std;
int main(){
    print_hello();
    return 0;
}

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

Thanks!

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9597
Re: compiles fine in command prompt, but code blocks complains
« Reply #3 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.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: http://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: http://wiki.codeblocks.org/index.php?title=FAQ

Offline Auria

  • Almost regular
  • **
  • Posts: 152
Re: compiles fine in command prompt, but code blocks complains
« Reply #4 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

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5247
Re: compiles fine in command prompt, but code blocks complains
« Reply #5 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 ??

Offline darkwalk

  • Multiple posting newcomer
  • *
  • Posts: 25
Re: compiles fine in command prompt, but code blocks complains
« Reply #6 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.

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5247
Re: compiles fine in command prompt, but code blocks complains
« Reply #7 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 ;-)

Offline darkwalk

  • Multiple posting newcomer
  • *
  • Posts: 25
Re: compiles fine in command prompt, but code blocks complains
« Reply #8 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.

Offline Biplab

  • Developer
  • Lives here!
  • *****
  • Posts: 1874
    • Biplab's Blog
Re: compiles fine in command prompt, but code blocks complains
« Reply #9 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
Be a part of the solution, not a part of the problem.

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5247
Re: compiles fine in command prompt, but code blocks complains
« Reply #10 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 !!!

Offline darkwalk

  • Multiple posting newcomer
  • *
  • Posts: 25
Re: compiles fine in command prompt, but code blocks complains
« Reply #11 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?

Offline Biplab

  • Developer
  • Lives here!
  • *****
  • Posts: 1874
    • Biplab's Blog
Re: compiles fine in command prompt, but code blocks complains
« Reply #12 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
Be a part of the solution, not a part of the problem.

Offline darkwalk

  • Multiple posting newcomer
  • *
  • Posts: 25
Re: compiles fine in command prompt, but code blocks complains
« Reply #13 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?

Offline darkwalk

  • Multiple posting newcomer
  • *
  • Posts: 25
Re: compiles fine in command prompt, but code blocks complains
« Reply #14 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?

Offline Biplab

  • Developer
  • Lives here!
  • *****
  • Posts: 1874
    • Biplab's Blog
Re: compiles fine in command prompt, but code blocks complains
« Reply #15 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.
Be a part of the solution, not a part of the problem.

Offline darkwalk

  • Multiple posting newcomer
  • *
  • Posts: 25
Re: compiles fine in command prompt, but code blocks complains
« Reply #16 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? 

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5247
Re: compiles fine in command prompt, but code blocks complains
« Reply #17 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.



Offline darkwalk

  • Multiple posting newcomer
  • *
  • Posts: 25
Re: compiles fine in command prompt, but code blocks complains
« Reply #18 on: May 23, 2007, 05:32:36 pm »
I just want to thank everyone who helped:

Biplab
killerbot
MortenMacFly

Offline darkwalk

  • Multiple posting newcomer
  • *
  • Posts: 25
Re: compiles fine in command prompt, but code blocks complains
« Reply #19 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!

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5247
Re: compiles fine in command prompt, but code blocks complains
« Reply #20 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 !!!

Offline darkwalk

  • Multiple posting newcomer
  • *
  • Posts: 25
Re: compiles fine in command prompt, but code blocks complains
« Reply #21 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.

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5247
Re: compiles fine in command prompt, but code blocks complains
« Reply #22 on: May 23, 2007, 05:55:10 pm »
perfect.

ENJOY CODE::BLOCKS !!!!!