CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > OpenFOAM Programming & Development

C++11 in OF

Register Blogs Members List Search Today's Posts Mark Forums Read

Like Tree4Likes
  • 2 Post By knuckles
  • 1 Post By wyldckat
  • 1 Post By knuckles

Reply
 
LinkBack Thread Tools Display Modes
Old   March 25, 2016, 16:49
Default C++11 in OF
  #1
Member
 
Join Date: Aug 2015
Posts: 35
Rep Power: 3
knuckles is on a distinguished road
I'm developing my own extensions to OpenFOAM and I'd like to user C++11 features. According to another thread [ http://www.cfd-online.com/Forums/ope...-features.html ], OF3.0 is built with native support for C++14. That kind of agrees with the release announcement:
"The release benefits from improved compatibility between the C++ compilers of GCC (4.5+, tested up to v5.2), Intel ICC (15.0.3+) and Clang (3.6+, 3.7 recommended), in line with the ISO 14882:2014(E) Programming Language C++ (C++14) Standard. Over time, we expect to introduce new features in the C++14 Standard to OpenFOAM, paying attention to compatibility of older versions of C++ compilers used in long-term supported Linux operating systems for high availability computers where possible." [ http://www.openfoam.org/version3.0.0/ ]
...however, when I try to use C++11 features in my code and compile with wmake, I get warnings and errors. I've grep'd through the source code... it appears that the "-std=c++0x" flag is set under some circumstances (which my code doesn't seem to fall under), and the '-std=c++1y" flag is never set. Reading the quotation above more closely, I don't think that they actually state that the code is compiled with C++14.

I know that I can make OF compile with a different C++ standard [ see http://www.sourceflux.de/blog/compil...upport-c11c0x/ , http://www.sourceflux.de/blog/enabli...cking-options/ ], but I will be running my code on other machines where I'm not able to re-compile OpenFOAM myself.

Is there a trick that I'm missing - is there a way that I can use C++11 features in my own code and link to standard OF libraries? I've found that enabling C++11 for my own code without recompiling OF leads to problems at the linking stage, and I'm not able to re-compile OF on all of the machines which I will be using.
knuckles is offline   Reply With Quote

Old   March 26, 2016, 13:55
Default
  #2
Member
 
Join Date: Aug 2015
Posts: 35
Rep Power: 3
knuckles is on a distinguished road
I've found the answer.
Additionally, this commit from Jan 23 [ https://github.com/OpenFOAM/OpenFOAM...1e9eba0e218168 ] enables C++11 support when compiling ThirdParty packages (e.g. paraview) which are not compiled with wmake. Presumably this change will also be reflected in future versions of OF (after 3.0.1).

I was able to make my own program compile with C++11 support in OF3.0.1 as follows:
  1. Backup $WM_PROJECT_DIR/wmake/rules/linux64Gcc/c++
  2. Add "-std=c++0x" (or "-std=c++1y" for C++14 support) to line 8 of the file from step 1
  3. wmake my own program without recompiling OF
I also assume that I could have also recompiled OF without issue, if I'd been inclined, but then OF 3.0.1 source doesn't use C++11 features so recompiling it shouldn't change the functionality of the resulting program. I had somehow gotten it into my head that there might be problems linking my program (compiled with C++11) to base libraries compiled without C++11, but this stack overflow question [ http://stackoverflow.com/questions/5...by-another-one ] makes it clear that there should be no problem... and that's my observation (everything links and runs just fine).

For comparison, the method described at http://www.sourceflux.de/blog/compil...upport-c11c0x/ does not work for me.
wyldckat and sina_mech like this.
knuckles is offline   Reply With Quote

Old   March 26, 2016, 14:25
Default
  #3
Super Moderator
 
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 9,800
Blog Entries: 39
Rep Power: 106
wyldckat is a glorious beacon of lightwyldckat is a glorious beacon of lightwyldckat is a glorious beacon of lightwyldckat is a glorious beacon of lightwyldckat is a glorious beacon of light
Greetings knuckles,

Many thanks for sharing!

I had seen this thread of yours earlier today, but I didn't have the time to test the following myself.
If you didn't need to rebuild OpenFOAM and you only had to update the respective c++ rules file, then you can simply make the following change in your application/library "Make/options" files:
Code:
EXE_INC = -std=c++0x \
    -I..... \
Namely adding the C++11 option to the "EXE_INC" line should do the same effect as modifying the rules files directly.

Best regards,
Bruno
sina_mech likes this.
__________________
wyldckat is offline   Reply With Quote

Old   March 26, 2016, 17:05
Default
  #4
Member
 
Join Date: Aug 2015
Posts: 35
Rep Power: 3
knuckles is on a distinguished road
Quote:
Originally Posted by wyldckat View Post
Greetings knuckles,

...you can simply make the following change in your application/library "Make/options" files:
Code:
EXE_INC = -std=c++0x \
    -I..... \
Namely adding the C++11 option to the "EXE_INC" line should do the same effect as modifying the rules files directly.
Thanks Bruno! I've given your suggestion a try. It's much easier, and it works perfectly for me.
wyldckat likes this.
knuckles is offline   Reply With Quote

Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 23:08.