News:

Accounts with zero posts and zero activity during the last months will be deleted periodically to fight SPAM!

Main Menu

compiles fine in command prompt, but code blocks complains

Started by darkwalk, May 23, 2007, 03:43:35 PM

Previous topic - Next topic

darkwalk

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!

killerbot

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

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!

MortenMacFly

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.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: [url="https://www.codeblocks.org/docs/main_codeblocks_en.html"]https://www.codeblocks.org/docs/main_codeblocks_en.html[/url]
C::B FAQ: [url="https://wiki.codeblocks.org/index.php?title=FAQ"]https://wiki.codeblocks.org/index.php?title=FAQ[/url]

Auria

Also if you get more errors can you post the exact error message and not some reformulation of your own?

thanks

killerbot

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

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

killerbot

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

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

Biplab

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.

killerbot

ok it fails for me too. Normal: you didn't add the printStuff.cpp and .h to the project !!!

darkwalk

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

Biplab

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

<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

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?