Author Topic: Examples  (Read 10634 times)

Offline ukimiku

  • Single posting newcomer
  • *
  • Posts: 8
Examples
« on: December 03, 2009, 01:18:49 pm »
Hi,

I would like to learn C++ with Code::Blocks. I have downloaded and installed C::B, and manually added the MINGW compiler suite.

I would now like to test whether my Code::Blocks/Compiler setup is ready to go. Could any of you please post a first little sample program (maybe a console app) to test whether it compiles, builds, and runs? I'm using Windows Vista (hence the manual MINGW installation).

Thank you!

Regards

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: Examples
« Reply #1 on: December 03, 2009, 01:23:37 pm »
Could any of you please post a first little sample program (maybe a console app) to test whether it compiles, builds, and runs?
Use the project wizard(s) to create yourself a console / any other app.
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 ukimiku

  • Single posting newcomer
  • *
  • Posts: 8
Re: Examples
« Reply #2 on: December 03, 2009, 04:25:14 pm »
Morten,

thank you. That was really over-obvious.
Anyhow, I ran into the problem that I, when attempting to compile even the tiniest Hello World sample, received a "...invalid compiler [YOUR ANSWER IS ALREADY THERE. SEARCH THE FORUMS!].. skipping..." message. Turned out there were two MINGW directories, one with gcc 4.4.0, one with 3.4.5. Setting the latter as default finally worked.

Now, anytime I create a new project with a console app and compile the program for the first time, the "Hello World" does not appear. Instead, the console window comes up, empty. I then close the console. When I compile the app again, the "Hello World" appears, the process terminates gracefully (exit code 0). Do you happen to know why I have to compile everything twice before it works?

Regards
« Last Edit: December 03, 2009, 04:31:50 pm by ukimiku »

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: Examples
« Reply #3 on: December 03, 2009, 04:34:47 pm »
Do you happen to know why I have to compile everything twice before it works?
To be honest: No. :( I never experienced such.
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 ukimiku

  • Single posting newcomer
  • *
  • Posts: 8
Re: Examples
« Reply #4 on: December 03, 2009, 05:25:48 pm »
Thanks for your help!
Regards

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7591
    • My Best Post
Re: Examples
« Reply #5 on: December 03, 2009, 05:44:25 pm »
Morten,

thank you. That was really over-obvious.
Anyhow, I ran into the problem that I, when attempting to compile even the tiniest Hello World sample, received a "...invalid compiler [YOUR ANSWER IS ALREADY THERE. SEARCH THE FORUMS!].. skipping..." message. Turned out there were two MINGW directories, one with gcc 4.4.0, one with 3.4.5. Setting the latter as default finally worked.

Now, anytime I create a new project with a console app and compile the program for the first time, the "Hello World" does not appear. Instead, the console window comes up, empty. I then close the console. When I compile the app again, the "Hello World" appears, the process terminates gracefully (exit code 0). Do you happen to know why I have to compile everything twice before it works?

Regards

NOTE: If you have two MinGW Installation; neither can be in C:\MinGW this folder is hard coded in some of the search paths.

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline ukimiku

  • Single posting newcomer
  • *
  • Posts: 8
Re: Examples
« Reply #6 on: December 03, 2009, 10:30:07 pm »
Tim,

thanks for replying. Yet I don't understand exactly what you are pointing at. I have two installations, and I changed both the Windows system PATH environment variable and the paths in the Code::Blocks application so that they now point to my preferred installation of MINGW. In fact, the program compiles and runs. So it would seem to me that you can choose any folder. Otherwise, what would be the use of being able to change the path to your installation in the configuration preferences, I wonder? Am I mistaken? In which search paths is "MINGW" hard-coded? Why should it be?

Thanks & regards!
« Last Edit: December 03, 2009, 10:37:14 pm by ukimiku »

Offline ukimiku

  • Single posting newcomer
  • *
  • Posts: 8
Re: Examples
« Reply #7 on: December 03, 2009, 10:33:23 pm »
Do you happen to know why I have to compile everything twice before it works?
To be honest: No. :( I never experienced such.


Morten, could it be that on Windows Vista, the usual console template fails to flush the output buffer? That would explain why I can't see any "Hello World" the first time I run the program. What do you think? I'm a novice at C++ and wouldn't know how to test whether my assumption is correct.

Regards

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Examples
« Reply #8 on: December 03, 2009, 10:48:00 pm »
I have a Vista installation on a virtual machine and it works fine here.
I use TDM's MinGW 4.4 and a selfbuild C::B.

The 8.02 release has an issue with the path set in the compiler-options. It uses the executables, that come first in the systems search-path, newer versions (aka nightly builds) always put the configured path in front of the systems search path.

Dou you use C::B as normal user or do you have administration rights ?
If you have multiple installations of MinGW in the searchpath, you might get problems if one of them lacks some executables and the files of the other installation are used.

Did you try to use a nightly-build and a clean installation of TDM's gcc 4.4 (and no other MinGW on your system), that is known to work correctly with Vista ?

nightlies: http://forums.codeblocks.org/index.php/board,20.0.html
TDM's gcc: http://tdragon.net/recentgcc/

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: Examples
« Reply #9 on: December 04, 2009, 06:38:32 am »
Morten, could it be that on Windows Vista, the usual console template fails to flush the output buffer?
Note sure what you mean by "termplate", but the console that opens when running the compiled "Hello World" executable shows everything. When the application terminates all buffers are flushed. Always. The reason is simple: They would not be available anmore otherwise after destruction... ;-).
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 ukimiku

  • Single posting newcomer
  • *
  • Posts: 8
Re: Examples
« Reply #10 on: December 04, 2009, 09:03:09 pm »
I installed the latest nightly build. And reinstalled the MINGW compiler manually from scratch, into the directory "C:\MinGW". Code::Blocks runs fine, but when trying to build and run the sample Hello World console app, "ld" complains: "-luser32" was missing. When I point the toolchain executables back to another directory on C: drive which contains my formerly installed MinGW, everything works fine (except that I have to compile everything twice to see Hello World actually printed in the console window).

What is -luser32? I did a search on C: drive, and it yielded hundreds of results, but none of the filenames contained "-luser32". Strange (?)

Thanks for your patience!

By the way, no matter which MinGW is set as the default, an attempt at "Rebuild" from the "Build" menu makes "ld" complain: "cannot open output file... Permission denied." I didn't change anything between the first "Build and run" and the "Rebuild". ??

Regards
« Last Edit: December 04, 2009, 09:05:59 pm by ukimiku »

zabzonk

  • Guest
Re: Examples
« Reply #11 on: December 04, 2009, 10:40:32 pm »
-luser 32 is part of the linker command line - it is saying "link the library called libuser32.a" - this is the MinGW-supplied export library for the Windows USER32.DLL system library.

It sounds as though your installation is completely screwed up. I would delete both MinGW installations (and maybe CB as well) and start again.  Install one copy of MinGW (I would use the Twilight Dragon branch) somewhere different - I put mine in a directory that includes its version number, for example C:\mingw440. Also, don't use mixed case in file or directory names - you will be thankful if you ever need to port to Linux/UNIX.

Offline ukimiku

  • Single posting newcomer
  • *
  • Posts: 8
Re: Examples
« Reply #12 on: December 04, 2009, 11:07:32 pm »
Zabzonk,

thanks for thinking with me.

Actually, the compilation even of GUI projects works just fine, just as long as I accept that I have to compile every program twice (it's fast enough not to make me yawn... so what the heck... I don't really mind...), but only if I switch back to my last-but-one MinGW installation (thanks for pointing me to the case issue in path names...)

Still, the behavior looks strange to some degree.

Well, I will now go about learning some basic of c++ and the write a simple .dll project (say one that adds two numbers... :) in Code::Blocks. If it should not work out due to compiler or MinGW setup issues, I will try a reinstall. Oops, wasn't that what I just did?

Regards,