Author Topic: c++11 compilation  (Read 11551 times)

Offline saperlipopette

  • Single posting newcomer
  • *
  • Posts: 6
c++11 compilation
« on: October 30, 2012, 08:21:09 pm »
Hi,
I work with ubuntu 12.04 64bits
I have newly installed g++ 4.7, (the one with C++11 features)
I can compile a simple program on the command line with this pattern:
Code
g++-4.7 -std=c++11 tst_threads2.cpp -o tst_threads2 -lpthread

After several tests, I manages to have codeBlock compiling the same way. I used a specific build command as shown in the screen capture below.


The code compiles now  :D
The problem is it does not link.
I get the following message:

Compiling: main2.cpp
Linking console executable: bin/Debug/essai1
g++: fatal error: -fuse-linker-plugin, but liblto_plugin.so not found
compilation terminated.

Please do not expect any basic knowledge on codeBlock from myself. I am totally new to codeBlock.
Actually, I am trying to have C++11 compiling on either codeBlock and Eclipse. I will stick to the first one that will be running.

Thanks for reading,
Olivier
Larry Wall - "True greatness is measured by how much freedom you give to others, not by how much you can coerce others to do what you want."

Offline Alpha

  • Developer
  • Lives here!
  • *****
  • Posts: 1513
Re: c++11 compilation
« Reply #1 on: October 30, 2012, 10:23:56 pm »
If you are using Code::Blocks 10.05, I would recommend first switching to this PPA.

Just as the red text in your screen shot says ;), this is not the location you want to be modifying settings in.  Switch it back to how it used to be.  Next open Settings->Compiler... find the "Compiler Flags" box (it should be right there) and check the one talking about C++11.  (If you decided to stick with Code::Blocks 10.05, the option will not be there, so add -std=c++11 to the "Other options" tab.)
Switch to the "Toolchain executables" tab and change all g++s to g++-4.7.

Offline saperlipopette

  • Single posting newcomer
  • *
  • Posts: 6
Re: c++11 compilation
« Reply #2 on: October 30, 2012, 11:27:43 pm »
Alpha,
Many thanks for helping.
I have spent the entire day posting my problem to all the forums I could find on the Internet and you are the first to make me make a step forward.
I still have questions:
* I plugged the PPA and made a sudo apt-get update. The system actually downloaded some stuff from the new PPA but CodeBlock is still the 10.05. What did I miss?
* Thanks to your advices, I succeeded at compiling AND linking my hello world project.
* BUT: This multithread project hangs when starting the thread.
This is the code
Code
#include <iostream>
#include <thread>

//This function will be called from a thread
void call_from_thread() {
    std::cout << "Hello, from thread!" << std::endl;
}

int main() {
    std::cout << "Hello, from main!" << std::endl;

    //Launch a thread
    std::thread t1(call_from_thread);

    //Join the thread with the main thread
    t1.join();


    return 0;
}
This is the output
Code
olivier@LISAH-OBOKERN:~/codeblocks_projects/essai1/bin/Debug$ ./essai1 
Hello, from main!
terminate called after throwing an instance of 'std::system_error'
  what():  Operation not permitted
Aborted (core dumped)

and this is the output when compiled from the console:
Code
olivier@LISAH-OBOKERN:~/codeblocks_projects/essai1$ g++-4.7 -std=c++11 main.cpp -o main -lpthread
olivier@LISAH-OBOKERN:~/codeblocks_projects/essai1$ ./main
Hello, from main!
Hello, from thread!
olivier@LISAH-OBOKERN:~/codeblocks_projects/essai1$

So, I conclude that something is still missing to CodeBlocks to allow multithreading programming. (single-thraded programs work fine, as far as I tested them)

Again, thanks for helping. I am new to CodeBlock, and so happy that some useful help came at last! :)
« Last Edit: October 30, 2012, 11:29:56 pm by saperlipopette »
Larry Wall - "True greatness is measured by how much freedom you give to others, not by how much you can coerce others to do what you want."

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: c++11 compilation
« Reply #3 on: October 31, 2012, 06:42:41 am »
So, I conclude that something is still missing to CodeBlocks to allow multithreading programming. (single-thraded programs work fine, as far as I tested them)
No, not at all. Please understand that Code::Blocks is an IDE that does only what you tell it to do. If you develop applications that crash its not the fault of Code::Blocks. Code::Clocks itself is heavily multi-threaded and is developed using guess what? Right: Code::Blocks.

So - grab yourself a book about thread programming and start reading. This forum is not dedicated to teach programming, its about the development and usage of Code::Blocks, the IDE.

Luckily there are plenty of forums around capturing such questions.

Also, start reading the manual to Code::Bocks itself, because setting up compilation in the single file's properties is absolutely wrong in the first place. You have project/target specific settings to do so.
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 saperlipopette

  • Single posting newcomer
  • *
  • Posts: 6
Re: c++11 compilation
« Reply #4 on: October 31, 2012, 11:02:56 am »
@MortenMacFly

As told in my last post, the same program compiles and runs from the command line (see the results in the last quoted block).
Only CodeBlocks cannot run it, and this is very likely because I missed something in CodeBlock settings, not in my 5-line piece of code that woks elsewhere.

Thanks, Olivier
Larry Wall - "True greatness is measured by how much freedom you give to others, not by how much you can coerce others to do what you want."

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: c++11 compilation
« Reply #5 on: October 31, 2012, 01:07:42 pm »
As told in my last post, the same program compiles and runs from the command line (see the results in the last quoted block).
Then inspect the full command lines of both compilation processes (which you didn't provide) and see the differences. Also make sure you are using the same compiler and libs. Finally verify you are starting in the same path in case you access files, for example.

There is technically not other reason than that why it behaves differently.
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 saperlipopette

  • Single posting newcomer
  • *
  • Posts: 6
Re: c++11 compilation
« Reply #6 on: October 31, 2012, 01:48:44 pm »
Then inspect the full command lines of both compilation processes (which you didn't provide) and see the differences. Also make sure you are using the same compiler and libs. Finally verify you are starting in the same path in case you access files, for example.

There is technically not other reason than that why it behaves differently.
Thanks for the tips.
Can you give me a clue of how to process these inspections? I'm totally new to CodeBlocks.
Regards, Olivier
Larry Wall - "True greatness is measured by how much freedom you give to others, not by how much you can coerce others to do what you want."

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: c++11 compilation
« Reply #7 on: October 31, 2012, 01:59:31 pm »
Can you give me a clue of how to process these inspections? I'm totally new to CodeBlocks.

> Then inspect the full command lines of both compilation processes [...]
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
...the console output you should have / see have.

> you are using the same compiler and libs
> starting in the same path in case you access files, for example.
I am not a Linux user - for this I cannot tell you exactly... except that they also matter on that platform. On Windows, I would use a process explorer to find out. Sorry.
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 saperlipopette

  • Single posting newcomer
  • *
  • Posts: 6
Re: c++11 compilation
« Reply #8 on: October 31, 2012, 08:23:30 pm »
Thanks very much for helping.
I have inspected the full command lines of both compilation projects as advised. They are different.

codeBlock does this (and it fails):
g++-4.7 -Wall -fexceptions  -g  -std=c++11 -lpthread    -c /home/olivier/codeblocks_projects/essai1/main.cpp -o obj/Debug/main.o
g++-4.7  -o bin/Debug/essai1 obj/Debug/main.o    

On the console, I do this (and it works):
g++-4.7 -std=c++11 /home/olivier/codeblocks_projects/essai1/main.cpp -o /home/olivier/codeblocks_projects/essai1/bin/Debug/main -lpthread

I understand very little to these lines. My past C++ experience was exclusively on Microsoft Visual Studio, where I simply ignored what was done in the background when clicking the build button.

Note that CodeBlock successfully build and link any single-threaded project. Only the multi-threaded projects hang.
« Last Edit: October 31, 2012, 08:26:09 pm by saperlipopette »
Larry Wall - "True greatness is measured by how much freedom you give to others, not by how much you can coerce others to do what you want."

Offline Alpha

  • Developer
  • Lives here!
  • *****
  • Posts: 1513
Re: c++11 compilation
« Reply #9 on: October 31, 2012, 10:17:12 pm »
g++-4.7 -Wall -fexceptions  -g  -std=c++11 -lpthread    -c /home/olivier/codeblocks_projects/essai1/main.cpp -o obj/Debug/main.o
It looks like you added -lpthread to the "Other options" tab, however, this is linker related (and the "Other options" tab is for compile time options).  Remove it, then under the "Linker settings" tab, add pthread to link libraries.

I understand very little to these lines. My past C++ experience was exclusively on Microsoft Visual Studio, where I simply ignored what was done in the background when clicking the build button.
I would recommend doing some background reading on the compile -> link process, if you are unfamiliar with it.

* I plugged the PPA and made a sudo apt-get update. The system actually downloaded some stuff from the new PPA but CodeBlock is still the 10.05. What did I miss?
I am relatively new to Ubuntu, but if I had to guess, this may be a 32 / 64 bit issue.  I think that PPA only has 32 bit packages, so you will probably have to uninstall your current Code::Blocks, then specifically install the 32 bit package.  ( -- Can any other Ubuntu users confirm this?)

* BUT: This multithread project hangs when starting the thread.
... technically, I do not think it should even have been able to compile... but who am I to argue with GCC ;).

By the way, all the directions I have been giving you have been for global settings.  In the long run, it is better practice to use project settings (except for the toolchain executables, which must be global... per-project toolchain executables makes no sense).

Offline saperlipopette

  • Single posting newcomer
  • *
  • Posts: 6
Re: c++11 compilation
« Reply #10 on: November 01, 2012, 10:11:10 am »
FIXED !  :-*  :-*

I have added /usr/lib/x86_64-linux-gnu/libpthread.so to the libraries to link.

Thank-you very very much to both of you for your help.  :-*
Larry Wall - "True greatness is measured by how much freedom you give to others, not by how much you can coerce others to do what you want."