Compiling GCC on OS X

本文介绍如何在Mac OS X上从源代码编译安装最新稳定版的GCC,并启用Graphite循环优化功能。文中详细介绍了所需步骤及依赖库的安装配置过程。

In this tutorial, I will show you how to compile from source and install the current stable version of GCC with Graphite loop optimizations on your OS X computer. The instructions from this tutorial were tested with Xcode 6.0.1 and Yosemite (OS X 10.10). Clang, the default compiler for OS X, supports only C, C++ and Objective-C. If you are interested in a modern Fortran compiler, for e.g., you will need gfortran that comes with GCC. Another reason to have the latest stable version of GCC on you Mac is that it provides you with an alternative C and C++ compiler. Testing your code with two different compilers is always a good idea. In order to compile GCC from sources you will need a working C++ compiler. In the remaining of this article I will assume that you have installed the Command Line Tools for Xcode. At the time of this writing Apple’s Command Line Tools maps the gcc and g++ to clang and clang++.

Let’s start by downloading the last stable version of GCC from the GNU website, so go to:http://gcc.gnu.org/mirrors.html and download gcc-4.9.2.tar.bz2. I’ve saved the archive in my Downloadsfolder. We will also need three other libraries for a successful build of gcc: mpcmpfr and gmp. Use the above links and download the last versions for all of them: gmp-6.0.0a.tar.bz2, mpc-1.0.2.tar.gz and mpfr-3.1.2.tar.bz2, also save them in your Downloads folder. For enabling the Graphite loop optimizations you will need two extra libraries, go toftp://gcc.gnu.org/pub/gcc/infrastructure/ and download isl-0.12.2.tar.bz2 and cloog-0.18.1.tar.gz.

Extract the above six archives in your Downloads folder and open a Terminal window.

We will start by compiling the gmp library:

1
2
3
cd ~
cd Downloads
cd gmp*

Create a new folder named build in which the compiler will save the compiled library:

1
mkdir build && cd build

And now the fun part … write in your Terminal:

1
../configure --prefix=/usr/gcc-4.9.2 --enable-cxx

If you see no error message we can actually compile the gmp library:

1
make -j 4

In a few minutes you will have a compiled gmp library. If you see no error message … congratulations, we are ready to install the library in the /usr/gcc-4.9.2 folder (you will need the administrator password for this):

1
sudo make install

We will do the same steps for MPFR now:

1
2
3
4
cd ..
cd ..
cd mpfr*
mkdir build && cd build

Configuration phase:

1
../configure --prefix=/usr/gcc-4.9.2 --with-gmp=/usr/gcc-4.9.2

The second parameter will just inform the configure app that gmp is already installed in /usr/gcc-4.9.2.

After the configure phase is finished, we can make and install the library:

1
2
make -j 4
sudo make install

Now, we are going to build MPC:

1
2
3
4
5
6
7
cd ..
cd ..
cd mpc*
mkdir build && cd build
../configure --prefix=/usr/gcc-4.9.2 --with-gmp=/usr/gcc-4.9.2 --with-mpfr=/usr/gcc-4.9.2
make -j 4
sudo make install

At this time you should have finished to build and install the necessary prerequisites for GCC.

Next step is to build the libraries for the Graphite loop optimizations:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
cd ..
cd ..
cd isl*
mkdir build && cd build
../configure --prefix=/usr/gcc-4.9.2 --with-gmp-prefix=/usr/gcc-4.9.2
make -j 4
sudo make install

cd ..
cd ..
cd cloog*
mkdir build && cd build
../configure --prefix=/usr/gcc-4.9.2 --with-gmp-prefix=/usr/gcc-4.9.2 --with-isl-prefix=/usr/gcc-4.9.2
make -j 4
sudo make install

We are ready to compile GCC now. Be prepared that this could take more than one hour on some machines … Since I’m interested only in the C, C++ and Fortran compilers, this is the configure command I’ve used on my machine:

1
2
3
4
5
cd ..
cd ..
cd gcc*
mkdir build && cd build
../configure --prefix=/usr/gcc-4.9.2 --enable-checking=release --with-gmp=/usr/gcc-4.9.2 --with-mpfr=/usr/gcc-4.9.2 --with-mpc=/usr/gcc-4.9.2 --enable-languages=c,c++,fortran --with-isl=/usr/gcc-4.9.2 --with-cloog=/usr/gcc-4.9.2 --program-suffix=-4.9.2

The above command instructs the configure app where we have installed gmp, mpfr, mpc, ppl and cloog; also it tells to add a prefix to all the resulting executable programs, so for example if you will invoke GCC 4.9.2 you will write gcc-4.9.2, the gcc command will invoke Apple’s version of clang.

If you are interested in building more compilers available in the GCC collection modify the –enable-languages configure option.

And now, the final touches:

1
make -j 4

Grab a coffee, maybe a book, and wait … this should take approximately, depending on your computer configuration, an hour … or more … and about 2GB of your disk space for the build folder.

Install the compiled gcc in /usr/gcc-4.9.2:

1
sudo make install

Now, you can keep the new compiler completely isolated from your Apple’s gcc compiler and, when you need to use it, just modify your path by writing in Terminal:

1
export PATH=/usr/gcc-4.9.2/bin:$PATH

If you want to avoid writing the above command each time you open a Terminal, save the above command in the file .bash_profile from your Home folder.

You should be able to invoke any of the newly compiled compilers C, C++, Fortran …, invoking g++ is as simple as writing in your Terminal:

1
g++-4.9.2 test.cpp -o test

Remember to erase your build directories from Downloads if you want to recover some space.

Let’s check if g++-4.9.2 can compile some C++11 specifics. In your favorite text editor, copy and save this test program (I’ll assume you will save the file in your Home directory):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
//Program to test the new C++11 lambda syntax and initializer lists
#include <iostream>
#include <vector>

using namespace std;

int main()
{
  // Test lambda
  cout << [](int m, int n) { return m + n;} (2,4) << endl;

  // Test initializer lists and range based for loop
  vector<int> V({1,2,3});

  cout << "V =" << endl;
  for(auto e : V) {
    cout << e << endl;
  }

  return 0;
}

Compiling and running the above lambda example:

1
2
3
4
5
6
7
g++-4.9.2 -std=c++11 tst_lambda.cpp -o tst_lambda
./tst_lambda
6
V =
1
2
3

We could also compile a C++ code that uses the new thread header from C++11:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
//Create a C++11 thread from the main program

#include <iostream>
#include <thread>

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

int main() {
    //Launch a thread
    std::thread t1(call_from_thread);

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

    return 0;
}

GCC 4.9.2, finally, implements the C++11 regex header. Next, we present a simple C++11 code that uses regular expressions to check if the input read from stdin is a floating point number:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//Uses a regex to check if the input is a floating point number

#include <iostream>
#include <regex>
#include <string>

using namespace std;

int main()
{
  string input;
  regex rr("((\\+|-)?[[:digit:]]+)(\\.(([[:digit:]]+)?))?((e|E)((\\+|-)?)[[:digit:]]+)?");
  //As long as the input is correct ask for another number
  while(true)
  {
    cout<<"Give me a real number!"<<endl;
    cin>>input;
    if(!cin) break;
    //Exit when the user inputs q
    if(input=="q")
      break;
    if(regex_match(input,rr))
      cout<<"float"<<endl;
    else
    {
      cout<<"Invalid input"<<endl;
    }
  }
}

If you are a Fortran programmer, you can use some of the Fortran 2008 features like do concurrent with gfortran-4.9.2:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
integer,parameter::mm=100000
real::a(mm), b(mm)
real::fact=0.5

! initialize the arrays
! ...

do concurrent (i = 1 : mm)
	a(i) = a(i) + b(i)
enddo

end

The above code can be compiled with (assuming you’ve named it tst_concurrent_do.f90):

1
2
gfortran-4.9.2 tst_concurrent_do.f90 -o tst_concurrent_do
./tst_concurrent_do


from https://solarianprogrammer.com/2013/06/11/compiling-gcc-mac-os-x/


内容概要:本文介绍了基于贝叶斯优化的CNN-LSTM混合神经网络在时间序列预测中的应用,并提供了完整的Matlab代码实现。该模型结合了卷积神经网络(CNN)在特征提取方面的优势与长短期记忆网络(LSTM)在处理时序依赖问题上的强大能力,形成一种高效的混合预测架构。通过贝叶斯优化算法自动调参,提升了模型的预测精度与泛化能力,适用于风电、光伏、负荷、交通流等多种复杂非线性系统的预测任务。文中还展示了模型训练流程、参数优化机制及实际预测效果分析,突出其在科研与工程应用中的实用性。; 适合人群:具备一定机器学习基基于贝叶斯优化CNN-LSTM混合神经网络预测(Matlab代码实现)础和Matlab编程经验的高校研究生、科研人员及从事预测建模的工程技术人员,尤其适合关注深度学习与智能优化算法结合应用的研究者。; 使用场景及目标:①解决各类时间序列预测问题,如能源出力预测、电力负荷预测、环境数据预测等;②学习如何将CNN-LSTM模型与贝叶斯优化相结合,提升模型性能;③掌握Matlab环境下深度学习模型搭建与超参数自动优化的技术路线。; 阅读建议:建议读者结合提供的Matlab代码进行实践操作,重点关注贝叶斯优化模块与混合神经网络结构的设计逻辑,通过调整数据集和参数加深对模型工作机制的理解,同时可将其框架迁移至其他预测场景中验证效果。
Rebuild started: Project: Project *** Using Compiler 'V6.19', folder: 'C:\Keil_v5\ARM\ARMCLANG\Bin' Rebuild target 'Project' compiling app_ble.c... ../Src/main.c(200): warning: null passed to a callee that requires a non-null argument [-Wnonnull] fputc('s', NULL); ~~~~^ ../Src/main.c(339): warning: null passed to a callee that requires a non-null argument [-Wnonnull] fputc('w', NULL); ~~~~^ 2 warnings generated. compiling main.c... ../Src/app_at.c(755): warning: passing 'char *' to parameter of type 'uint8_t *' (aka 'unsigned char *') converts between pointers to integer types where one is of the unique plain 'char' type and the other is not [-Wpointer-sign] tcp_rec_show_update((char *)p->payload,p->len); ^~~~~~~~~~~~~~~~~~ ../Src/app_at.c(754): note: passing argument to parameter 'data' here extern void tcp_rec_show_update(uint8_t *data,uint32_t len); ^ 1 warning generated. compiling app_at.c... compiling app_btdm.c... ../Src/app_bt.c(662): warning: format specifies type 'char *' but the argument has type 'void *' [-Wformat] printf("hfg codec connection rsp: %s,type = %d\r\n",Info->p.ptr,codec_type); ~~ ^~~~~~~~~~~ 1 warning generated. compiling app_bt.c... compiling app_task.c... compiling app_hw.c... compiling app_lvgl.c... compiling diskio.c... compiling user_bt.c... compiling autonavi_handler.c... compiling autonavi_profile.c... compiling app_rpmsg.c... compiling msbc_sample.c... compiling app_hid_sdp.c... compiling app_audio.c... compiling sbc_sample.c... compiling mp3_sample.c... compiling btdm_mem.c... compiling controller.c... assembling controller_code.s... compiling host.c... assembling img_rom.s... compiling SWD.c... compiling fal_flash_port.c... compiling fdb_app.c... assembling startup_fr30xx.s... compiling driver_cali.c... compiling trim_fr30xx.c... compiling system_fr30xx.c... compiling driver_efuse.c... compiling driver_frspim.c... compiling driver_flash.c... compiling driver_gpio.c... compiling driver_pmu.c... compiling driver_pmu_iwdt.c... compiling driver_qspi.c... compiling driver_uart.c... compiling driver_trng.c... compiling driver_timer.c... compiling driver_dma.c... compiling driver_display.c... compiling driver_spi_master.c... compiling driver_sd_card.c... compiling driver_sd.c... compiling driver_ipc.c... compiling driver_can.c... compiling driver_pdm.c... compiling driver_i2s.c... compiling driver_psd_dac.c... compiling driver_display_dev.c... compiling driver_saradc.c... compiling driver_st7282_rgb_hw.c... compiling ext_flash.c... compiling co_log.c... ../../../../components/drivers/bsp/spi_flash/IC_W25Qxx.c(240): warning: incompatible pointer types passing 'uint16_t *' (aka 'unsigned short *') to parameter of type 'uint8_t *' (aka 'unsigned char *') [-Wincompatible-pointer-types] __SPI_Read_flash_X1(lu8_DataBuffer, 4, pu8_Buffer, fu32_Length); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ../../../../components/drivers/bsp/spi_flash/IC_W25Qxx.h(37): note: expanded from macro '__SPI_Read_flash_X1' #define __SPI_Read_flash_X1(__CMD__, __CSIZE__, __BUFFER__, __SIZE__) spi_master_readflash_X1(&spi_flash_handle, (uint16_t *)__CMD__, __CSIZE__, (void *)__BUFFER__, __SIZE__) ^~~~~~~~~~~~~~~~~~~ ../../../../components/drivers/peripheral/Inc/driver_spi.h(522): note: passing argument to parameter 'fp_CMD_ADDR' here void spi_master_readflash_X1(SPI_HandleTypeDef *hspi, uint8_t *fp_CMD_ADDR, uint32_t fu32_CMDLegnth, uint8_t *fp_Data, uint16_t fu16_Size); ^ 1 warning generated. compiling IC_W25Qxx.c... compiling co_list.c... compiling fal.c... compiling co_util.c... compiling fal_flash.c... compiling fdb.c... compiling fal_partition.c... compiling croutine.c... compiling fdb_utils.c... compiling event_groups.c... compiling list.c... compiling fdb_kvdb.c... compiling stream_buffer.c... compiling queue.c... compiling timers.c... compiling port.c... compiling portasm.c... compiling heap_6.c... assembling cpu_context.s... compiling tasks.c... compiling freertos_sleep.c... compiling heap.c... compiling lv_indev.c... compiling lv_refr.c... compiling lv_obj_pos.c... compiling lv_flex.c... compiling lv_bmp.c... compiling lv_grid.c... compiling lv_ffmpeg.c... compiling lv_freetype.c... compiling lv_fs_fatfs.c... compiling lv_fs_posix.c... compiling lv_fs_stdio.c... compiling lv_fs_win32.c... compiling lv_gif.c... compiling gifdec.c... compiling lodepng.c... compiling lv_png.c... compiling lv_qrcode.c... compiling lv_rlottie.c... compiling lv_sjpg.c... compiling tjpgd.c... compiling lv_fragment.c... compiling lv_fragment_manager.c... compiling lv_gridnav.c... compiling lv_ime_pinyin.c... compiling qrcodegen.c... compiling lv_imgfont.c... compiling lv_msg.c... compiling lv_monkey.c... compiling lv_snapshot.c... compiling lv_theme_basic.c... compiling lv_theme_mono.c... compiling lv_animimg.c... compiling lv_theme_default.c... compiling lv_calendar.c... compiling lv_calendar_header_arrow.c... compiling lv_calendar_header_dropdown.c... compiling lv_imgbtn.c... compiling lv_colorwheel.c... compiling lv_chart.c... compiling lv_keyboard.c... compiling lv_led.c... compiling lv_list.c... compiling lv_menu.c... compiling lv_msgbox.c... compiling lv_meter.c... compiling lv_spinner.c... compiling lv_spinbox.c... compiling lv_span.c... compiling lv_tileview.c... compiling lv_win.c... compiling lv_tabview.c... compiling lv_font.c... compiling lv_extra.c... compiling lv_font_dejavu_16_persian_hebrew.c... compiling lv_font_fmt_txt.c... compiling lv_font_montserrat_8.c... compiling lv_font_loader.c... compiling lv_font_montserrat_10.c... compiling lv_font_montserrat_12.c... compiling lv_font_montserrat_12_subpx.c... compiling lv_font_montserrat_14.c... compiling lv_font_montserrat_16.c... compiling lv_font_montserrat_18.c... compiling lv_font_montserrat_20.c... compiling lv_font_montserrat_22.c... compiling lv_font_montserrat_24.c... compiling lv_font_montserrat_26.c... compiling lv_font_montserrat_28_compressed.c... compiling lv_font_montserrat_28.c... compiling lv_font_montserrat_30.c... compiling lv_font_montserrat_32.c... compiling lv_font_montserrat_34.c... compiling lv_font_montserrat_36.c... compiling lv_font_montserrat_38.c... compiling lv_font_montserrat_40.c... compiling lv_font_montserrat_42.c... compiling lv_font_montserrat_44.c... compiling lv_font_montserrat_48.c... compiling lv_font_montserrat_46.c... compiling lv_font_simsun_16_cjk.c... compiling lv_font_unscii_8.c... compiling lv_font_unscii_16.c... compiling lv_hal_tick.c... compiling lv_hal_indev.c... compiling lv_hal_disp.c... compiling lv_anim.c... compiling lv_anim_timeline.c... compiling lv_area.c... compiling lv_async.c... compiling lv_color.c... compiling lv_bidi.c... compiling lv_fs.c... compiling lv_gc.c... compiling lv_log.c... compiling lv_ll.c... compiling lv_math.c... compiling lv_lru.c... compiling lv_printf.c... compiling lv_mem.c... compiling lv_templ.c... compiling lv_style.c... compiling lv_style_gen.c... compiling lv_tlsf.c... compiling lv_timer.c... compiling lv_utils.c... compiling lv_txt.c... compiling lv_txt_ap.c... compiling lv_arc.c... compiling lv_btn.c... compiling lv_bar.c... compiling lv_checkbox.c... compiling lv_canvas.c... compiling lv_btnmatrix.c... compiling lv_img.c... compiling lv_dropdown.c... compiling lv_objx_templ.c... compiling lv_label.c... compiling lv_line.c... compiling lv_slider.c... compiling lv_switch.c... compiling lv_roller.c... compiling lv_table.c... compiling lv_textarea.c... compiling lv_demo_benchmark.c... compiling img_benchmark_cogwheel_alpha16.c... compiling img_benchmark_cogwheel_argb.c... compiling img_benchmark_cogwheel_chroma_keyed.c... compiling img_benchmark_cogwheel_indexed16.c... compiling img_benchmark_cogwheel_rgb.c... compiling img_benchmark_cogwheel_rgb565a8.c... compiling lv_font_bechmark_montserrat_12_compr_az.c.c... compiling lv_font_bechmark_montserrat_16_compr_az.c.c... compiling lv_font_bechmark_montserrat_28_compr_az.c.c... compiling lv_demo_stress.c... compiling img_clothes.c... compiling lv_demo_widgets.c... compiling img_demo_widgets_avatar.c... compiling img_lvgl_logo.c... compiling lv_demo_music.c... compiling lv_demo_music_list.c... compiling img_lv_demo_music_btn_corner_large.c... compiling img_lv_demo_music_btn_list_pause.c... compiling lv_demo_music_main.c... compiling img_lv_demo_music_btn_list_pause_large.c... compiling img_lv_demo_music_btn_list_play_large.c... compiling img_lv_demo_music_btn_list_play.c... compiling img_lv_demo_music_btn_loop.c... compiling img_lv_demo_music_btn_loop_large.c... compiling img_lv_demo_music_btn_next.c... compiling img_lv_demo_music_btn_next_large.c... compiling img_lv_demo_music_btn_pause.c... compiling img_lv_demo_music_btn_pause_large.c... compiling img_lv_demo_music_btn_play.c... compiling img_lv_demo_music_btn_play_large.c... compiling img_lv_demo_music_btn_prev.c... compiling img_lv_demo_music_btn_prev_large.c... compiling img_lv_demo_music_btn_rnd.c... compiling img_lv_demo_music_btn_rnd_large.c... compiling img_lv_demo_music_corner_left.c... compiling img_lv_demo_music_corner_left_large.c... compiling img_lv_demo_music_corner_right.c... compiling img_lv_demo_music_corner_right_large.c... compiling img_lv_demo_music_cover_1.c... compiling img_lv_demo_music_cover_1_large.c... compiling img_lv_demo_music_cover_2.c... compiling img_lv_demo_music_cover_2_large.c... compiling img_lv_demo_music_cover_3.c... compiling img_lv_demo_music_cover_3_large.c... compiling img_lv_demo_music_icon_1.c... compiling img_lv_demo_music_icon_1_large.c... compiling img_lv_demo_music_icon_2.c... compiling img_lv_demo_music_icon_2_large.c... compiling img_lv_demo_music_icon_3.c... compiling img_lv_demo_music_icon_3_large.c... compiling img_lv_demo_music_icon_4.c... compiling img_lv_demo_music_icon_4_large.c... compiling img_lv_demo_music_list_border.c... compiling img_lv_demo_music_list_border_large.c... compiling img_lv_demo_music_logo.c... compiling img_lv_demo_music_slider_knob.c... compiling img_lv_demo_music_slider_knob_large.c... compiling img_lv_demo_music_wave_bottom.c... compiling img_lv_demo_music_wave_bottom_large.c... compiling img_lv_demo_music_wave_top.c... compiling crc32.c... compiling ffsystem.c... compiling img_lv_demo_music_wave_top_large.c... compiling ffunicode.c... compiling lfs_util.c... compiling ext_flash_program.c... compiling ext_flash_uart.c... compiling ff.c... compiling lfs_port.c... compiling lv_common_function.c... compiling batt_full_yellow.c... compiling lfs.c... compiling batt_full_gren.c... compiling arialuni_bbp1_32px__.c... compiling Number_HarmonyOS_bpp4_16px.c... compiling Number_HarmonyOS_bpp4_12px.c... compiling Number_HarmonyOS_bpp4_36px.c... compiling Number_HarmonyOS_bpp4_44px.c... compiling Number_HarmonyOS_bpp4_92px.c... compiling Number_HarmonyOS_bpp4_20px.c... compiling fr_lv_test_page.c... compiling fr_lv_86box_page.c... compiling fr_lv_app_music_control.c... compiling fr_lv_customer_page.c... compiling fr_lv_instrument_panel.c... compiling fr_lv_list_page.c... compiling fr_lv_instrument_panel_km.c... compiling fr_lv_can_page.c... compiling fr_lv_bt_pan_page.c... compiling fr_guimain.c... compiling fr_watch.c... compiling lv_user_sqlist.c... compiling fr_device_button.c... compiling fr_device_rtc.c... compiling fr_device_encode.c... compiling fr_device_pmu_io.c... compiling fr_device_canfd.c... compiling autoip.c... compiling icmp.c... compiling etharp.c... compiling igmp.c... compiling dhcp.c... compiling ip4.c... compiling ip4_addr.c... compiling dhcp6.c... compiling ethip6.c... compiling ip4_frag.c... compiling icmp6.c... compiling inet6.c... compiling ip6.c... compiling ip6_addr.c... compiling ip6_frag.c... compiling mld6.c... compiling nd6.c... compiling altcp.c... compiling altcp_alloc.c... compiling altcp_tcp.c... compiling def.c... compiling inet_chksum.c... compiling init.c... compiling dns.c... compiling ip.c... compiling mem.c... compiling memp.c... compiling raw.c... compiling netif.c... compiling stats.c... compiling sys.c... compiling pbuf.c... compiling tcp_in.c... compiling tcp.c... compiling tcp_out.c... compiling timeouts.c... compiling udp.c... compiling api_lib.c... compiling err.c... compiling if_api.c... compiling netbuf.c... compiling api_msg.c... compiling netdb.c... compiling netifapi.c... compiling tcpip.c... compiling ethernet.c... compiling ethernetif.c... compiling sys_arch.c... compiling sockets.c... compiling rpmsg.c... compiling rpmsg_ns.c... compiling rpmsg_lite.c... compiling llist.c... compiling rpmsg_queue.c... compiling virtqueue.c... compiling rpmsg_env_freertos.c... compiling rpmsg_platform.c... assembling dsp_code_rom.s... compiling dsp.c... compiling dsp_mem.c... compiling audio_a2dp_sink.c... compiling audio_a2dp_source.c... compiling audio_encoder.c... compiling audio_rpmsg.c... ../../../../components/modules/audio/audio_decoder.c(72): warning: null passed to a callee that requires a non-null argument [-Wnonnull] fputc(hex2char[(value >> 12)&0xf], NULL); ~~~~^ ../../../../components/modules/audio/audio_decoder.c(73): warning: null passed to a callee that requires a non-null argument [-Wnonnull] fputc(hex2char[(value >> 8)&0xf], NULL); ~~~~^ ../../../../components/modules/audio/audio_decoder.c(74): warning: null passed to a callee that requires a non-null argument [-Wnonnull] fputc(hex2char[(value >> 4)&0xf], NULL); ~~~~^ ../../../../components/modules/audio/audio_decoder.c(75): warning: null passed to a callee that requires a non-null argument [-Wnonnull] fputc(hex2char[(value >> 0)&0xf], NULL); ~~~~^ 4 warnings generated. compiling audio_decoder.c... compiling audio_hw.c... compiling audio_scene.c... compiling local_playback.c... compiling audio_sco.c... compiling loopback.c... compiling recorder.c... compiling voice_recognize.c... compiling algorithm.c... compiling codec.c... compiling resample.c... compiling AMS_client.c... compiling simple_gatt_service.c... compiling ANCS_AMS_client.c... compiling hid_service.c... compiling retarget_io.c... linking... ..\..\..\..\components\tools\keil\xip_flash_add_psram.sct(39): warning: L6329W: Pattern algorithm.o(RO) only matches removed unused sections. ..\..\..\..\components\tools\keil\xip_flash_add_psram.sct: Error: L6220E: Load region LR_IROM1 size (1300776 bytes) exceeds limit (1040384 bytes). Region contains 2110 bytes of padding and 2090 bytes of veneers (total 4200 bytes of linker generated content). ..\..\..\..\components\tools\keil\xip_flash_add_psram.sct: Error: L6221E: Execution region RW_RAM_CODE with Execution range [0x1ffe01b0,0x20000c3c) overlaps with Execution region RW_IRAM1 with Execution range [0x20000000,0x2005cb30). ..\..\..\..\components\tools\keil\xip_flash_add_psram.sct: Error: L6388E: ScatterAssert expression ((ImageLength(RW_RAM_CODE_FRONT) + ImageLength(RW_RAM_CODE)) <= 0x20000) failed on line 52 : (0x20c30 <= 0x20000) Finished: 0 information, 1 warning and 3 error messages. ".\Objects\Project.axf" - 3 Error(s), 10 Warning(s). Target not created. Build Time Elapsed: 00:01:15什么问题
09-13
<think> 首先,用户的问题是关于解决代码编译中的一系列错误和警告:空指针传递警告、指针类型不兼容警告、加载区域大小超出限制错误、执行区域重叠错误和ScatterAssert表达式失败错误。用户提供了四个引用,我需要参考这些引用,但用户说“上述引用仅供参考,并不是用户真正的需求”,我应该更多参考用户上一次的问题和我的回答。用户上一次的问题是我刚开始的,没有历史记录,因为这是第一条消息。 我的系统指令指定了格式:使用$...$进行行内数学表达式,$$...$$进行独立公式,使用中文回答,生成相关问题。最后,在回答中引用的段落末尾自然地添加引用标识。 用户提供的引用: 1. 关于IDEA和Eclipse编译差异的解决方法:建议将IDEA的编译器从javac改为Eclipse的JDT。 2. 关于MDK编译错误:找不到main.o和未定义错误,需要添加宏定义STM32F429xx。 3. 关于海思平台添加ffmpeg静态库报错:静态库链接顺序问题,调整链接顺序解决。 4. 关于Qt屏蔽未使用变量报警错误:没有给出具体内容,但提到了解决方法。 用户真正的需求是解决编译错误和警告:空指针传递警告、指针类型不兼容警告、加载区域大小超出限制错误、执行区域重叠错误、ScatterAssert表达式失败错误。 这些错误听起来更像是嵌入式系统或C/C++编程中的错误,可能与内存分配、链接器错误相关。用户提到了“ScatterAssert”,这可能是ARM工具链中的scatter file错误。 我需要基于这些错误提供解决方法。由于引用不是直接的,但我可以参考它们的思路:例如,检查编译设置、添加宏定义、调整链接顺序等。 结构化我的回答: 1. **引言**:简要说明编译错误的原因和一般解决方法。 2. **针对每个错误/警告的解决方法**: - 空指针传递警告:通常发生在传递NULL或未初始化的指针时。建议检查指针初始化和使用前检查是否为NULL。 - 指针类型不兼容警告:涉及指针类型转换,可能导致未定义行为。建议使用正确的类型转换或修复数据类型。 - 加载区域大小超出限制错误:可能是内存区域定义太小,无法容纳代码或数据。需要增大区域大小或优化代码。 - 执行区域重叠错误:在链接描述文件中,执行区域重叠导致冲突。需要调整内存布局。 - ScatterAssert表达式失败错误:ARM链接器错误,通常与scatter file中的断言失败相关。需要检查scatter file逻辑。 3. **一般建议**:参考引用中的思路,如检查编译设置、添加宏定义、调整链接顺序。 4. **生成相关问题**:根据指令,在结束前生成相关问题。 使用中文回答。 添加引用标识:如果我参考引用中的内容,在末尾添加 [^n],但用户说引用仅供参考,不是真正需求,所以我可能不需要直接引用它们,除非相关。 例如,在提到调整链接顺序时,可以引用[3]。 确保回答真实可靠:基于编程知识。 大纲回答: - 解释常见原因和解决方法。 - 每个错误点: - 空指针传递警告:检查代码以确保指针有效。 - 指针类型不兼容警告:强制类型转换可能导致问题,使用reinterpret_cast或检查类型。 - 加载区域大小超出限制:修改链接脚本增加大小。 - 执行区域重叠:修改内存映射避免重叠。 - ScatterAssert:检查scatter file语法和逻辑。 - 一般技巧:如编译设置、调试工具。 - 相关问题:生成2-3个相关问题。 </think> ### 解决编译错误与警告的方法 编译过程中出现的空指针传递警告、指针类型不兼容警告、加载区域大小超出限制错误、执行区域重叠错误和ScatterAssert表达式失败错误,通常源于代码逻辑漏洞、内存配置错误或编译器/链接器设置问题。这些错误在嵌入式系统开发(如使用ARM工具链)中较为常见。以下是针对每个问题的逐步解决方法,基于常见编程实践和工具链特性。解决方法需结合代码审查、编译设置调整和链接脚本优化。 #### 1. **空指针传递警告** - **原因**:代码中传递了未初始化或可能为NULL的指针,编译器检测到潜在的空指针解引用风险。这在C/C++中可能导致运行时崩溃。 - **解决方法**: - 使用前检查指针有效性:确保指针在使用前被初始化为有效地址。例如: ```c void func(int *ptr) { if (ptr != NULL) { // 添加空指针检查 *ptr = 10; // 安全访问 } } ``` - 初始化指针:声明指针时赋予初始值(如`int *ptr = &variable;`),避免野指针。 - 编译器选项调整:在IDE(如Keil或IAR)中,禁用不必要的严格警告。例如,在MDK中检查预处理器宏定义,确保未屏蔽NULL检查逻辑[^2]。 #### 2. **指针类型不兼容警告** - **原因**:不同类型指针(如`int*`和`char*`)之间进行隐式强制转换,编译器警告可能导致数据损坏或未定义行为。 - **解决方法**: - 使用显式类型转换:在C++中优先使用`static_cast`或`reinterpret_cast`,而非C风格转换。例如: ```c char *char_ptr = reinterpret_cast<char*>(int_ptr); // 明确转换类型 ``` - 检查类型一致性:确保函数参数和调用处的指针类型匹配。使用`void*`时,需谨慎处理。 - 编译器设置:在IDEA或Eclipse等IDE中,调整编译器严格度(如将javac改为JDT以减少误报),但需确保类型安全[^1]。 #### 3. **加载区域大小超出限制错误** - **原因**:链接器脚本(如ARM的scatter file)中定义的加载区域(Load Region)太小,无法容纳编译后的代码或数据段。常见于嵌入式系统内存受限环境。 - **解决方法**: - 增大加载区域大小:修改链接描述文件(如`.ld`或`.sct`),增加`Size`属性。例如: ``` LR_IROM1 0x08000000 0x10000 { // 增大大小至0x10000 ER_IROM1 0x08000000 0x10000 { *.o (RESET, +First) } } ``` - 优化代码大小:移除未使用代码(如通过编译器选项`-ffunction-sections`和链接器选项`--gc-sections`),或压缩数据。 - 检查内存映射:确保分区合理,参考设备手册(如STM32)的内存布局[^2]。 #### 4. **执行区域重叠错误** - **原因**:链接器脚本中定义的执行区域(Execution Region)地址范围有重叠,导致代码或数据覆盖冲突。 - **解决方法**: - 调整区域地址:在scatter file中确保所有执行区域互不重叠。例如: ``` ER_IROM1 0x08000000 0x8000 { ... } // 区域1 ER_IRAM1 0x20000000 0x2000 { ... } // 区域2移至非重叠地址 ``` - 使用工具验证:在Keil MDK中运行`Linker Map`报告,查看内存分配是否冲突。 - 宏定义添加:如引用[2]所示,确保预定义宏(如`STM32F429xx`)正确设置,以避免隐式布局错误[^2]。 #### 5. **ScatterAssert表达式失败错误** - **原因**:ARM链接器的散射文件(scatter file)中使用了`ScatterAssert`宏进行条件检查,但表达式失败(如地址越界或大小不匹配)。 - **解决方法**: - 检查ScatterAssert逻辑:确保表达式基于有效内存范围。例如,修改断言: ``` ScatterAssert(ImageLimit(ER_IROM1) <= 0x08010000) // 修正上限值 ``` - 调整链接顺序:类似引用[3]中解决静态库依赖的方法,确保关键库或对象文件加载顺序正确(如将入口点对象放首位)[^3]。 - 简化散射文件:移除不必要断言或拆分复杂表达式,逐步调试。 ### 一般最佳实践 - **编译器设置调整**:在IDE(如IDEA或Keil)中检查编译选项。例如: - 在IDEA中,将编译器从javac切换到Eclipse JDT以减少误报(路径:File → Settings → Build, Execution, Deployment → Compiler → Java Compiler)[^1]。 - 在MDK中,添加设备宏定义(如`STM32F429xx`)以避免未定义错误[^2]。 - **工具链使用**:利用调试工具(如`arm-none-eabi-size`)分析内存占用;静态代码分析器(如Cppcheck)捕获空指针隐患。 - **代码审查**:定期检查指针使用和内存分配。对于未使用变量警告,可添加`__attribute__((unused))`(GCC)或编译器选项屏蔽(参考Qt方法[^4])。 - **测试验证**:编译后运行单元测试,确认错误消除。 遵循以上步骤,大多数编译问题可解决。如果错误持续,提供具体代码片段或工具链信息以进一步诊断。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值