AVR Studio 5:编译C++代码

11

在创建 AVR Studio 5 项目时,它会创建一个包含以下内容的 .c 文件:

#include <avr/io.h>

int main(void)
{
    while(1)
    {
        //TODO:: Please write your application code 
    }
}

编译这个C程序没有问题:

------ Rebuild All started: Project: AVRGCC2, Configuration: Debug AVR ------
Build started.
Project "AVRGCC2.avrgccproj" (ReBuild target(s)):
Target "PreBuildEvent" skipped, due to false condition; ('$(PreBuildEvent)'!='') was evaluated as (''!='').
Target "CoreRebuild" in file "C:\Programme\Atmel\AVR Studio 5.0\Vs\AvrGCC.targets" from project "C:\Dokumente und Einstellungen\tom\Eigene Dateien\AVRStudio\test\AVRGCC2\AVRGCC2\AVRGCC2.avrgccproj" (target "ReBuild" depends on it):
    Task "RunAvrGCC"
        C:\Programme\Atmel\AVR Studio 5.0\AVR ToolChain\bin\make.exe clean all 
        rm -rf  AVRGCC2.o AVRGCC2.d   libAVRGCC2.a  AVRGCC2.hex  AVRGCC2.eep  AVRGCC2.lss AVRGCC2.map
AVRGCC2.c
        Invoking: AVR/GNU C Compiler
        "C:/Programme/Atmel/AVR Studio 5.0/AVR ToolChain/bin/avr-gcc.exe" -funsigned-char -funsigned-bitfields -O0 -fpack-struct -fshort-enums -g2 -Wall -c -std=gnu99  -mmcu=atmega328p   -MD -MP -MF"AVRGCC2.d" -MT"AVRGCC2.d" -o"AVRGCC2.o" ".././AVRGCC2.c"
        Finished building: .././AVRGCC2.c
        Building target: AVRGCC2.elf
        Invoking: AVR/GNU C/C++ Linker
        "C:/Programme/Atmel/AVR Studio 5.0/AVR ToolChain/bin/avr-gcc.exe"  -mmcu=atmega328p  -Wl,-Map=AVRGCC2.map -o AVRGCC2.elf  AVRGCC2.o  
        Finished building target: AVRGCC2.elf
        "C:/Programme/Atmel/AVR Studio 5.0/AVR ToolChain/bin/avr-objcopy.exe" -O ihex -R .eeprom -R .fuse -R .lock -R .signature  "AVRGCC2.elf" "AVRGCC2.hex"
        "C:/Programme/Atmel/AVR Studio 5.0/AVR ToolChain/bin/avr-objdump.exe" -h -S "AVRGCC2.elf" > "AVRGCC2.lss"
        "C:/Programme/Atmel/AVR Studio 5.0/AVR ToolChain/bin/avr-objcopy.exe" -j .eeprom --set-section-flags=.eeprom=alloc,load --change-section-lma .eeprom=0 --no-change-warnings -O ihex "AVRGCC2.elf" "AVRGCC2.eep" || exit 0
        AVR Memory Usage
        ----------------
        Device: atmega328p
        Program:     142 bytes (0.4% Full)
        (.text + .data + .bootloader)
        Data:          0 bytes (0.0% Full)
        (.data + .bss + .noinit)
    Done executing task "RunAvrGCC".
Done building target "CoreRebuild" in project "AVRGCC2.avrgccproj".
Target "PostBuildEvent" skipped, due to false condition; ('$(PostBuildEvent)' != '') was evaluated as ('' != '').
Target "ReBuild" in file "C:\Programme\Atmel\AVR Studio 5.0\Vs\Avr.common.targets" from project "C:\Dokumente und Einstellungen\tom\Eigene Dateien\AVRStudio\test\AVRGCC2\AVRGCC2\AVRGCC2.avrgccproj" (entry point):
Done building target "ReBuild" in project "AVRGCC2.avrgccproj".
Done building project "AVRGCC2.avrgccproj".

Build succeeded.
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

但是,当我将文件扩展名更改为 .cpp 后,构建过程会发出警告设备类型未定义(如果尝试使用任何 AVR 特定寄存器,则此警告将变成错误):

------ Rebuild All started: Project: AVRGCC2, Configuration: Debug AVR ------
Build started.
Project "AVRGCC2.avrgccproj" (ReBuild target(s)):
Target "PreBuildEvent" skipped, due to false condition; ('$(PreBuildEvent)'!='') was evaluated as (''!='').
Target "CoreRebuild" in file "C:\Programme\Atmel\AVR Studio 5.0\Vs\AvrGCC.targets" from project "C:\Dokumente und Einstellungen\tom\Eigene Dateien\AVRStudio\test\AVRGCC2\AVRGCC2\AVRGCC2.avrgccproj" (target "ReBuild" depends on it):
    Task "RunAvrGCC"
        C:\Programme\Atmel\AVR Studio 5.0\AVR ToolChain\bin\make.exe clean all 
        In file included from .././AVRGCC2.cpp:1:0:
c:\programme\atmel\avr studio 5.0\avr toolchain\bin\../lib/gcc/avr/4.5.1/../../../../avr/include/avr/io.h(446,6): #warning "device type not defined"
        rm -rf  AVRGCC2.o AVRGCC2.d   libAVRGCC2.a  AVRGCC2.hex  AVRGCC2.eep  AVRGCC2.lss AVRGCC2.map
AVRGCC2.cpp
        Invoking: AVR/GNU CPP Compiler
        "C:/Programme/Atmel/AVR Studio 5.0/AVR ToolChain/bin/avr-g++.exe"   -MD -MP -MF"AVRGCC2.d" -MT"AVRGCC2.d" -o"AVRGCC2.o" ".././AVRGCC2.cpp"
        Finished building: .././AVRGCC2.cpp
        Building target: AVRGCC2.elf
        Invoking: AVR/GNU C/C++ Linker
        "C:/Programme/Atmel/AVR Studio 5.0/AVR ToolChain/bin/avr-gcc.exe"  -mmcu=atmega328p  -Wl,-Map=AVRGCC2.map -o AVRGCC2.elf  AVRGCC2.o  
        Finished building target: AVRGCC2.elf
        "C:/Programme/Atmel/AVR Studio 5.0/AVR ToolChain/bin/avr-objcopy.exe" -O ihex -R .eeprom -R .fuse -R .lock -R .signature  "AVRGCC2.elf" "AVRGCC2.hex"
        "C:/Programme/Atmel/AVR Studio 5.0/AVR ToolChain/bin/avr-objdump.exe" -h -S "AVRGCC2.elf" > "AVRGCC2.lss"
        "C:/Programme/Atmel/AVR Studio 5.0/AVR ToolChain/bin/avr-objcopy.exe" -j .eeprom --set-section-flags=.eeprom=alloc,load --change-section-lma .eeprom=0 --no-change-warnings -O ihex "AVRGCC2.elf" "AVRGCC2.eep" || exit 0
        AVR Memory Usage
        ----------------
        Device: atmega328p
        Program:     142 bytes (0.4% Full)
        (.text + .data + .bootloader)
        Data:          0 bytes (0.0% Full)
        (.data + .bss + .noinit)
    Done executing task "RunAvrGCC".
Done building target "CoreRebuild" in project "AVRGCC2.avrgccproj".
Target "PostBuildEvent" skipped, due to false condition; ('$(PostBuildEvent)' != '') was evaluated as ('' != '').
Target "ReBuild" in file "C:\Programme\Atmel\AVR Studio 5.0\Vs\Avr.common.targets" from project "C:\Dokumente und Einstellungen\tom\Eigene Dateien\AVRStudio\test\AVRGCC2\AVRGCC2\AVRGCC2.avrgccproj" (entry point):
Done building target "ReBuild" in project "AVRGCC2.avrgccproj".
Done building project "AVRGCC2.avrgccproj".

Build succeeded.
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

比较这两个输出结果可以发现,对于C代码,它会在执行avr-gcc.exe时添加选项-mmcu=atmega328p,但对于C++代码则不会。在使用WinAVR时也遇到了类似的问题。该如何解决?


1
我找到了一个解决方法:将所有的.cpp文件重命名为.c,并定义以下编译器选项:-x c++ -std=gnu++98 - Mot
你能否把你的评论发布为答案并接受它呢?这样问题就不会继续显示为未回答了。 - Peter G.
3个回答

7
我找到了一个解决方法:将所有的 .cpp 文件重命名为 .c 并定义以下编译器选项:-x c++ -std=gnu++98

5
我意识到这是一个晚期的补充,但如果有其他人遇到了同样的问题:

您是否注意到在AVR Studio 5的工具 -> 扩展管理器下有一个C++扩展可用?这使您可以创建带有.cpp扩展名的C++项目和所有内容。源代码


0
你尝试过打开项目对话框并检查设备类型是否设置了吗?

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接