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

C++11 in OF

Register Blogs Community New Posts Updated Threads Search

Like Tree7Likes
  • 2 Post By knuckles
  • 3 Post By wyldckat
  • 2 Post By knuckles

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   March 25, 2016, 16:49
Default C++11 in OF
  #1
Member
 
Join Date: Aug 2015
Posts: 37
Rep Power: 11
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: 37
Rep Power: 11
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
Retired Super Moderator
 
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 10,982
Blog Entries: 45
Rep Power: 128
wyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to allwyldckat is a name known to all
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
__________________
wyldckat is offline   Reply With Quote

Old   March 26, 2016, 17:05
Default
  #4
Member
 
Join Date: Aug 2015
Posts: 37
Rep Power: 11
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 and HappyS5 like this.
knuckles is offline   Reply With Quote

Old   July 11, 2017, 05:15
Default
  #5
New Member
 
Thien Xuan Dinh
Join Date: Jul 2014
Location: Japan
Posts: 8
Rep Power: 12
dinh is on a distinguished road
It is a late thank. But it is great help.
dinh is offline   Reply With Quote

Reply


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 Off
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 04:37.