MacOS 下 C/CPP 开发

发布于 / Code / 0 条评论

##安装 Xcode
在 App Store 中搜索 Xcode 并安装

测试环境

这里我写了一个 Demo 用来测试,代码如下:

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#ifndef var
#define var     __auto_type
#endif

/// noreturn测试函数
static int noreturn MyTest(void)
{
    const bool equal = sizeof(long) == sizeof(int);
    printf("Is equal? %s\n", equal ? "Yes" : "No");

    // 原子操作测试
    volatile atomic_int atom;
    atomic_init(&atom, 0);
    const var oldValue = atomic_fetch_add(&atom, 100);
    printf("Previous value = %d, current value = %d\n", oldValue, atomic_load(&atom));

    volatile atomic_flag flag = ATOMIC_FLAG_INIT;
    if(!atomic_flag_test_and_set(&flag))
        puts("Access the resource...");
    else
        puts("Locked!");

    if(!atomic_flag_test_and_set(&flag))
        puts("Access the resource...");
    else
        puts("Locked!");

    atomic_flag_clear(&flag);
    if(!atomic_flag_test_and_set(&flag))
        puts("Access the resource...");
    else
        puts("Locked!");

    exit(0);
}

int main(int argc, const char * argv[])
{
    // 字符编码测试
    const char *utf8Str = u8"你好,世界!";
    printf("The string is: %s\n", utf8Str);

    const var utf16Char = u'¥';
    printf("The UTF-16 code is: %.4X\n", utf16Char);

    const char encoding = _Generic(utf16Char, uint8_t:'c',
                                   uint16_t:'s', uint32_t:'i', default:'o');

    printf("The type of utf16Char is: %c\n", encoding);

    // 复数测试
    complex float comp1 = 5.0f + 3.0fi;
    complex float comp2 = {3.0f, -2.0f};
    comp1 -= comp2;
    printf("comp1 real = %f, imag = %f\n", __real(comp1), __imag(comp1));

    // 静态断言测试
    static_assert(sizeof(long) == 8, "Not 64-bit environment!");

    return MyTest();
}

问题

  • 在 Xcode 无法使用 bits/stdc++.h,需要自行配置。
转载原创文章请注明,转载自: 风过不留痕 » MacOS 下 C/CPP 开发
Not Comment Found