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

darkwalk

  • Guest
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: 5490
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 ??

darkwalk

  • Guest
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
#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!

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
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: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://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: 5490
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 ??

darkwalk

  • Guest
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: 5490
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 ;-)

darkwalk

  • Guest
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: 5490
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 !!!

darkwalk

  • Guest
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.

darkwalk

  • Guest
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?

darkwalk

  • Guest
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?