Opened 10 years ago

Closed 10 years ago

#7671 closed Bugs (fixed)

Error including boost/thread.hpp header on iOS

Reported by: Dr.Schroers@… Owned by: viboes
Milestone: Boost 1.53.0 Component: thread
Version: Boost 1.52.0 Severity: Problem
Keywords: Cc: viboes

Description

System configuration: MacOS X 10.8.2, Xcode 4.5.2, iOS SDK 6.0, iOS 6.0.1 installed on device, boost v1.52.0 compiled via the script at http://gitorious.org/~galbraithjoseph/boostoniphone/galbraithjosephs-boostoniphone All latest patches and updates installed.

Setup: I have successfully built the boost.framework using the above script and included it into an iOS project with a test target. One test case class includes the file <boost.thread.hpp>.

Expected behavior: The test target is compiled and run on the device.

Observed behavior: Compilation fails with three times the error message "Semantic issue - Constexpr function never produces a constant expression". In all three cases the issue appears to be due to the same piece of code included in the following hierarchy:

boost/thread.hpp line 13:
boost/thread/thread.hpp line 17:
boost/thread/pthread/thread_data.hpp line 11:
boost/thread/locks.hpp line 11:
boost/chrono/time_point.hpp line 18:
boost/chrono/duration.hpp line 33:
boost/chrono/duration.hpp lines 355/363/371:
limits line 443

Comment: This applies only to the thread.hpp header. In the test case object I have also included the <boost.regex.hpp> and the <boost/lambda/lambda.hpp> headers. These two are correctly included and the corresponding test cases work as expected. Only the thread.hpp header shows the issue described above.

Attachments (2)

BoostTest.tgz (8.0 KB ) - added by Wolfram Schroers <Dr.Schroers@…> 10 years ago.
Sample project that demonstrates the error
libcpp_c11_numeric_limits.patch (401 bytes ) - added by Hyeon-bin Jeong <tuhertz@…> 10 years ago.
Set BOOST_NO_CXX11_NUMERIC_LIMITS for Xcode 4.5 to fix compilation error.

Download all attachments as: .zip

Change History (22)

comment:1 by viboes, 10 years ago

Could you show the exact command and the compiler error and the source file?

comment:2 by viboes, 10 years ago

I guess that you should link with libc++ which is C++11 aware.

comment:3 by Wolfram Schroers <Dr.Schroers@…>, 10 years ago

I could not access the specific command the Xcode IDE issues (I will try it with xcodebuild on the command line later), but the error message in the header boost/chrono/duration.hpp says "Non-constexpr function 'max' cannot be used in constant expression".

I just noticed that the issue goes away if I remove BOOST_CHRONO_LIB_CONSTEXPR in lines 353, 361 and 369 in the file boost/chrono/duration.hpp and the project compiles fine.

in reply to:  2 ; comment:4 by Wolfram Schroers <Dr.Schroers@…>, 10 years ago

Replying to viboes: The project uses C++11 and the compiler supports the constexpr statement. The issue seems to be that the boost-header assumes that std::numeric_limits<float>::max is a compile-time constant which is not the case on the given platform.

in reply to:  4 ; comment:5 by viboes, 10 years ago

Replying to Wolfram Schroers <Dr.Schroers@…>:

Replying to viboes: The project uses C++11 and the compiler supports the constexpr statement. The issue seems to be that the boost-header assumes that std::numeric_limits<float>::max is a compile-time constant which is not the case on the given platform.

As said before you need to use a std lib compatible with C++11. libc++ is free and works well with clang.

in reply to:  5 ; comment:6 by Wolfram Schroers <Dr.Schroers@…>, 10 years ago

Replying to viboes: [...]

As said before you need to use a std lib compatible with C++11. libc++ is free and works well with clang.

In the default answer of http://stackoverflow.com/questions/12865226/in-xcode-4-5-what-is-compiler-default-for-c-standard-library-and-c-lan?rq=1 this post there is specific code to test for stdlib++ vs. lib++. The output for me is:

Using libc++
Language mode = gnu++11

I am not sure how to proceed in this case, because it looks like I am already using libc++.

in reply to:  6 comment:7 by viboes, 10 years ago

Replying to Wolfram Schroers <Dr.Schroers@…>:

Replying to viboes: [...]

As said before you need to use a std lib compatible with C++11. libc++ is free and works well with clang.

In the default answer of http://stackoverflow.com/questions/12865226/in-xcode-4-5-what-is-compiler-default-for-c-standard-library-and-c-lan?rq=1 this post there is specific code to test for stdlib++ vs. lib++. The output for me is:

Using libc++
Language mode = gnu++11

I am not sure how to proceed in this case, because it looks like I am already using libc++.

Hrrr. I'm using MacOS 10.7.5 and when I compile for C++11 and use libc++ I don't get any error.

Which version of libc++ are you using? Which version of the compiler?

comment:8 by viboes, 10 years ago

The source of libc++ <limits< contains the following

template <class _Tp>
class _LIBCPP_VISIBLE numeric_limits
    : private __libcpp_numeric_limits<typename remove_cv<_Tp>::type>
{
    typedef __libcpp_numeric_limits<typename remove_cv<_Tp>::type> __base;
    typedef typename __base::type type;
public:
    static _LIBCPP_CONSTEXPR const bool is_specialized = __base::is_specialized;
    _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR type min() _NOEXCEPT {return __base::min();}
    _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR type max() _NOEXCEPT {return __base::max();}

with specializations for float, double and long double

template <>
class __libcpp_numeric_limits<float, true>
{
protected:
...
    _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR type min() _NOEXCEPT {return __FLT_MIN__;}
    _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR type max() _NOEXCEPT {return __FLT_MAX__;}

I don't see why then the error occurs. Please could you verify the code inside <limits> on your configuration?

in reply to:  8 comment:9 by Wolfram Schroers <Dr.Schroers@…>, 10 years ago

Replying to viboes:

In my case the file contains:

template <class _Tp>
class _LIBCPP_VISIBLE numeric_limits
    : private __libcpp_numeric_limits<typename remove_cv<_Tp>::type>
{
    typedef __libcpp_numeric_limits<typename remove_cv<_Tp>::type> __base;
    typedef typename __base::type type;
public:
    static const bool is_specialized = __base::is_specialized;
    _LIBCPP_INLINE_VISIBILITY static type min() _NOEXCEPT {return __base::min();}
    _LIBCPP_INLINE_VISIBILITY static type max() _NOEXCEPT {return __base::max();}
    _LIBCPP_INLINE_VISIBILITY static type lowest() _NOEXCEPT {return __base::lowest();}

I have created a minimal Xcode project file that shows the error when compiling and attached the file to this report. The version numbers are stated in the original report. Hope that helps ...

by Wolfram Schroers <Dr.Schroers@…>, 10 years ago

Attachment: BoostTest.tgz added

Sample project that demonstrates the error

comment:10 by rkb, 10 years ago

Hello -- came across this thread during search for issue.

From my xcode experiment to build basic thread program, ran into same issue.

On my end - I switched to C++ Standard Library libstdc++ and was able to compile my program. Earlier errors were the same as above.

in reply to:  10 comment:11 by Wolfram Schroers <Dr.Schroers@…>, 10 years ago

Replying to rkb: [...]

On my end - I switched to C++ Standard Library libstdc++ and was able to compile my program. Earlier errors were the same as above.

I have just explicitly added libstdc++.dylib to the "Link Binary With Libraries" section of the "Build Phases" tab. The result is unchanged, however. As far as I can see, the .dylib library does not come with a separate header file and the issue is in the header limits.

Could you be a little more explicit in describing how you switched the library?

comment:12 by rkb, 10 years ago

Hello. My apologies. To detail a bit more:

Under project build settings in xcode, try searching for "lib" via the search field. Under Apple LLVM compiler 4.1, a field called C++ Standard Library appears. Just click this to toggle to libstdc++ (GNU C++ standard library).

It was after this change that I was able to successfully compile. Please let me know if this is helpful.

comment:13 by tuhertz, 10 years ago

libc++ in Xcode is outdated , so constexpr keyword missing in limits header. It should work fine if you change C++ language dialect to C++98 or GNU++98. If you want to compile Boost with C++11 dialect, you need to get latest headers from libc++ trunk.

comment:14 by Wolfram Schroers <Dr.Schroers@…>, 10 years ago

Changing the libc++ to libstdc++ in the "C++ Standard Library" row in the "Build Settings" yields 50+ linker errors. This does not appear to work.

I also do not feel comfortable installing custom headers for what feels like a basic system library. There may be potential side effects, including the danger that some modification under the hood could endanger app store approval or something ...

The only practical solution I have found so far is to edit the boost header <boost/thread.hpp> and remove the BOOST_CHRONO_LIB_CONSTEXPR keyword.

comment:15 by tuhertz, 10 years ago

If you don't change C++ Language Dialect in Build Setting, you will get errors no matter which library you choose. Because Boost knows your compiler supports C+11 constexpr, but both libc++ and libstdc++ don't implement it in limits header. Changing language dialect to C++98 is easiest fix if you don't have to build Boost with C++11.

I think building with custom limits header won't make any trouble, because limits is header only library and all classes are empty classes. All members are static or inline methods, so it will just replace with constants in Boost library in compilation time. You don't need to replacing system header, just adding it's location to Header Search Path in build settings, it takes precedence over system header.

libc++ has been updated in Xcode 4.6 developer preview and i confirm limit header is updated also, so this problem will be gone in next Xcode release.

by Hyeon-bin Jeong <tuhertz@…>, 10 years ago

Set BOOST_NO_CXX11_NUMERIC_LIMITS for Xcode 4.5 to fix compilation error.

comment:16 by viboes, 10 years ago

Cc: viboes added
Component: threadconfig
Owner: changed from Anthony Williams to John Maddock

comment:17 by Dr. Wolfram Schroers <Dr.Schroers@…>, 10 years ago

Thanks for your responses. In my situation switching to Xcode 4.6 is the best choice at the moment. The alternative patch file is also a good alternative, but I'll go with switching to the developer preview for now.

comment:18 by me@…, 10 years ago

You can also add #define BOOST_THREAD_DONT_USE_CHRONO before including the Boost headers for this error to go away.

comment:19 by viboes, 10 years ago

Component: configthread
Owner: changed from John Maddock to viboes
Status: newassigned

[82391] Should have fixed this issue.

comment:20 by viboes, 10 years ago

Milestone: To Be DeterminedBoost 1.53.0
Resolution: fixed
Status: assignedclosed
Note: See TracTickets for help on using tickets.