macOS:编译boost静态库 error:implicit declaration of function ‘out_printf‘ is invalid in C99

在macOS下,使用brew安装的Boost默认为动态库。为获取静态库版本,用户手动下载Boost1.62进行编译,但在执行./bootstrap.sh时遇到错误,原因是AppleClang编译器对未声明函数的处理更严格。根据文章,此问题是由于高版本AppleClang导致。升级到Boost1.75.0后,通过./bootstrap.sh和特定参数编译安装,成功生成了静态库文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

macOS下如果你使用brew安装boost,很方便

brew install boost

但默认安装出来的boost 1.75是动态库,如果想要静态库版本还是要自己编译。

于是我到boost官网下载了boost 1.62

在执行./bootstrap.sh失败了。
查看./bootstrap.log,如下:

###
### Using 'darwin' toolset.
###
rm -rf bootstrap
mkdir bootstrap
cc -o bootstrap/jam0 command.c compile.c constants.c debug.c execcmd.c frames.c function.c glob.c hash.c hdrmacro.c headers.c jam.c jambase.c jamgram.c lists.c make.c make1.c object.c option.c output.c parse.c pathsys.c regexp.c rules.c scan.c search.c subst.c timestamp.c variable.c modules.c strings.c filesys.c builtins.c class.c cwd.c native.c md5.c w32_getreg.c modules/set.c modules/path.c modules/regex.c modules/property-set.c modules/sequence.c modules/order.c execunix.c fileunix.c pathunix.c
execcmd.c:120:5: error: implicit declaration of function 'out_printf' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    out_printf( "...interrupted\n" );
    ^
1 error generated.
make.c:132:13: error: implicit declaration of function 'out_printf' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
            out_printf( "...found %d target%s...\n", counts->targets,
            ^
make.c:184:17: error: implicit declaration of function 'out_printf' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
                out_printf( "fate change  %s from %s to %s (as dependant of %s)\n",
                ^
make.c:215:17: error: implicit declaration of function 'out_printf' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
                out_printf( "fate change  %s from %s to %s (by rebuild)\n",
                ^
make.c:300:9: error: implicit declaration of function 'out_printf' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
        out_printf( "make\t--\t%s%s\n", spaces( depth ), object_str( t->name ) );
        ^
make.c:300:41: warning: adding 'int' to a string does not append to the string [-Wstring-plus-int]
        out_printf( "make\t--\t%s%s\n", spaces( depth ), object_str( t->name ) );
                                        ^~~~~~~~~~~~~~~
make.c:85:44: note: expanded from macro 'spaces'
#define spaces(x) ( "                    " + ( x > 20 ? 0 : 20-x ) )
                    ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
make.c:300:41: note: use array indexing to silence this warning
make.c:85:44: note: expanded from macro 'spaces'
#define spaces(x) ( "                    " + ( x > 20 ? 0 : 20-x ) )
                                           ^
make.c:307:41: warning: adding 'int' to a string does not append to the string [-Wstring-plus-int]
        out_printf( "make\t--\t%s%s\n", spaces( depth ), object_str( t->name ) );
                                        ^~~~~~~~~~~~~~~
make.c:85:44: note: expanded from macro 'spaces'
#define spaces(x) ( "                    " + ( x > 20 ? 0 : 20-x ) )
                    ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
make.c:307:41: note: use array indexing to silence this warning
make.c:85:44: note: expanded from macro 'spaces'
#define spaces(x) ( "                    " + ( x > 20 ? 0 : 20-x ) )
                                           ^
make.c:380:49: warning: adding 'int' to a string does not append to the string [-Wstring-plus-int]
            out_printf( "bind\t--\t%s%s: %s\n", spaces( depth ),
                                                ^~~~~~~~~~~~~~~
make.c:85:44: note: expanded from macro 'spaces'
#define spaces(x) ( "                    " + ( x > 20 ? 0 : 20-x ) )
                    ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
make.c:380:49: note: use array indexing to silence this warning
make.c:85:44: note: expanded from macro 'spaces'
#define spaces(x) ( "                    " + ( x > 20 ? 0 : 20-x ) )
                                           ^
make.c:388:49: warning: adding 'int' to a string does not append to the string [-Wstring-plus-int]
            out_printf( "time\t--\t%s%s: %s\n", spaces( depth ),
                                                ^~~~~~~~~~~~~~~
make.c:85:44: note: expanded from macro 'spaces'
#define spaces(x) ( "                    " + ( x > 20 ? 0 : 20-x ) )
                    ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
make.c:388:49: note: use array indexing to silence this warning
make.c:85:44: note: expanded from macro 'spaces'
#define spaces(x) ( "                    " + ( x > 20 ? 0 : 20-x ) )
                                           ^
make.c:393:49: warning: adding 'int' to a string does not append to the string [-Wstring-plus-int]
            out_printf( "time\t--\t%s%s: %s\n", spaces( depth ),
                                                ^~~~~~~~~~~~~~~
make.c:85:44: note: expanded from macro 'spaces'
#define spaces(x) ( "                    " + ( x > 20 ? 0 : 20-x ) )
                    ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
make.c:393:49: note: use array indexing to silence this warning
make.c:85:44: note: expanded from macro 'spaces'
#define spaces(x) ( "                    " + ( x > 20 ? 0 : 20-x ) )
                                           ^
make.c:735:13: error: implicit declaration of function 'out_flush' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
            out_flush();
            ^
make.c:758:13: warning: adding 'int' to a string does not append to the string [-Wstring-plus-int]
            spaces( depth ), object_str( t->name ) );
            ^~~~~~~~~~~~~~~
make.c:85:44: note: expanded from macro 'spaces'
#define spaces(x) ( "                    " + ( x > 20 ? 0 : 20-x ) )
                    ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
make.c:758:13: note: use array indexing to silence this warning
make.c:85:44: note: expanded from macro 'spaces'
#define spaces(x) ( "                    " + ( x > 20 ? 0 : 20-x ) )
                                           ^
6 warnings and 5 errors generated.
filesys.c:360:10: error: implicit declaration of function 'filelist_empty' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    if ( filelist_empty( archive->members ) )
         ^
filesys.c:496:10: error: implicit declaration of function 'filelist_empty' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    if ( filelist_empty( list ) )
         ^
filesys.c:534:10: error: implicit declaration of function 'filelist_empty' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    if ( filelist_empty( list ) )
         ^
filesys.c:570:10: error: implicit declaration of function 'filelist_empty' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    if ( filelist_empty( list ) ) return list;
         ^
filesys.c:593:11: error: implicit declaration of function 'filelist_empty' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    if ( !filelist_empty( list ) ) result = list->size;
          ^
filesys.c:602:10: error: implicit declaration of function 'filelist_empty' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    if ( filelist_empty( list ) ) return;
         ^
6 errors generated.
modules/path.c:16:12: error: implicit declaration of function 'file_query' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    return file_query( list_front( lol_get( frame->args, 0 ) ) ) ?
           ^
1 error generated.
fileunix.c:233:10: error: implicit declaration of function 'filelist_empty' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    if ( filelist_empty( archive->members ) )
         ^
fileunix.c:235:14: error: implicit declaration of function 'file_collect_archive_content_' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
        if ( file_collect_archive_content_( archive ) < 0 )
             ^
fileunix.c:235:14: note: did you mean 'file_collect_dir_content_'?
fileunix.c:110:5: note: 'file_collect_dir_content_' declared here
int file_collect_dir_content_( file_info_t * const d )
    ^
fileunix.c:294:12: error: implicit declaration of function 'filelist_empty' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    if ( ! filelist_empty( archive->members ) ) filelist_free( archive->members );
           ^
3 errors generated.

按照这篇文章的说明

《boost-1.61.0 failure to bootstrap with xcode-12 due to implicit function declarations》

应该是高版本的AppleClang编译器的带来的问题

Clang now reports an error when you use a function without an explicit declaration when building C or Objective-C code for macOS (-Werror=implicit-function-declaration flag is on). This additional error detection unifies Clang’s behavior for iOS/tvOS and macOS 64-bit targets for this diagnostic. (49917738)

1.62.0版本确实比较老了,那么我升级boost该可以解决问题吧?于是我下载了最新的 boost 1.75.0版本

https://jaist.dl.sourceforge.net/project/boost/boost/1.75.0/

再次执行成功了

./bootstrap.sh
## 编译并安装到$install_path指定的位置
./b2 cxxflags=-fPIC cflags=-fPIC --prefix=$install_path -q --debug-configuration link=static install

查看安装的路径,生成全部是 .a 静态库文件

ls -l $install_path/lib
total 38976
drwxr-xr-x 41 guyadong staff 1312 4 15 15:02 ./
drwxr-xr-x 4 guyadong staff 128 4 15 15:02 …/
drwxr-xr-x 50 guyadong staff 1600 4 15 15:02 cmake/
-rw-r–r-- 1 guyadong staff 42688 4 15 15:02 libboost_atomic.a
-rw-r–r-- 1 guyadong staff 70256 4 15 15:02 libboost_chrono.a
-rw-r–r-- 1 guyadong staff 114240 4 15 15:02 libboost_container.a
-rw-r–r-- 1 guyadong staff 39024 4 15 15:02 libboost_context.a
-rw-r–r-- 1 guyadong staff 137016 4 15 15:02 libboost_contract.a
-rw-r–r-- 1 guyadong staff 45952 4 15 15:02 libboost_coroutine.a
-rw-r–r-- 1 guyadong staff 912 4 15 15:02 libboost_date_time.a
-rw-r–r-- 1 guyadong staff 1008 4 15 15:02 libboost_exception.a
-rw-r–r-- 1 guyadong staff 193584 4 15 15:02 libboost_filesystem.a
-rw-r–r-- 1 guyadong staff 603216 4 15 15:02 libboost_graph.a
-rw-r–r-- 1 guyadong staff 135120 4 15 15:02 libboost_iostreams.a
-rw-r–r-- 1 guyadong staff 1067248 4 15 15:01 libboost_locale.a
-rw-r–r-- 1 guyadong staff 1937032 4 15 15:01 libboost_log.a
-rw-r–r-- 1 guyadong staff 2120192 4 15 15:01 libboost_log_setup.a
-rw-r–r-- 1 guyadong staff 225464 4 15 15:01 libboost_math_c99.a
-rw-r–r-- 1 guyadong staff 135632 4 15 15:01 libboost_math_c99f.a
-rw-r–r-- 1 guyadong staff 172696 4 15 15:01 libboost_math_c99l.a
-rw-r–r-- 1 guyadong staff 1115784 4 15 15:01 libboost_math_tr1.a
-rw-r–r-- 1 guyadong staff 893792 4 15 15:02 libboost_math_tr1f.a
-rw-r–r-- 1 guyadong staff 1058784 4 15 15:02 libboost_math_tr1l.a
-rw-r–r-- 1 guyadong staff 65768 4 15 15:02 libboost_numpy27.a
-rw-r–r-- 1 guyadong staff 86832 4 15 15:02 libboost_prg_exec_monitor.a
-rw-r–r-- 1 guyadong staff 668504 4 15 15:02 libboost_program_options.a
-rw-r–r-- 1 guyadong staff 455088 4 15 15:02 libboost_python27.a
-rw-r–r-- 1 guyadong staff 24320 4 15 15:02 libboost_random.a
-rw-r–r-- 1 guyadong staff 1074400 4 15 15:02 libboost_regex.a
-rw-r–r-- 1 guyadong staff 687536 4 15 15:02 libboost_serialization.a
-rw-r–r-- 1 guyadong staff 13312 4 15 15:02 libboost_stacktrace_addr2line.a
-rw-r–r-- 1 guyadong staff 7464 4 15 15:02 libboost_stacktrace_basic.a
-rw-r–r-- 1 guyadong staff 2456 4 15 15:02 libboost_stacktrace_noop.a
-rw-r–r-- 1 guyadong staff 880 4 15 15:01 libboost_system.a
-rw-r–r-- 1 guyadong staff 1438584 4 15 15:02 libboost_test_exec_monitor.a
-rw-r–r-- 1 guyadong staff 121784 4 15 15:02 libboost_thread.a
-rw-r–r-- 1 guyadong staff 29880 4 15 15:02 libboost_timer.a
-rw-r–r-- 1 guyadong staff 53280 4 15 15:02 libboost_type_erasure.a
-rw-r–r-- 1 guyadong staff 1426984 4 15 15:02 libboost_unit_test_framework.a
-rw-r–r-- 1 guyadong staff 3149560 4 15 15:02 libboost_wave.a
-rw-r–r-- 1 guyadong staff 447832 4 15 15:02 libboost_wserialization.a

使用的编译器为XCode内置的gcc编译器(Apple clang)

$ g++ -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.0 (clang-1200.0.32.29)
Target: x86_64-apple-darwin20.3.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

10km

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值