课上测试:C编程工具测试

作业题⽬:C编程⼯具测试(AI)

完成下⾯任务(29 分)

0. 在 Ubuntu 活 openEuler 中完成任务(推荐openEuler)

1. 编写C代码hello.c,打印“hello 你的 8 位学号”,给出编译预处理,编译,汇编,链接的gcc命令,及其运⾏结果,使⽤ git 记录过程。(5 分)

在openEuler中创建一个名为hello.c的文件,内容如下:

#include <stdio.h> 
 
int main() { 
    printf("hello 你的 8 位学号\n"); 
    return 0; 
} 

编译预处理、编译、汇编、链接的gcc命令

root@LAPTOP-PRC71A0C:~/wzy/ch2/hello# vim hello.c  //编写hello.c
root@LAPTOP-PRC71A0C:~/wzy/ch2/hello# gcc -E hello.c -o hello.i
root@LAPTOP-PRC71A0C:~/wzy/ch2/hello# gcc -S hello.i -o hello.s
root@LAPTOP-PRC71A0C:~/wzy/ch2/hello# gcc -c hello.s -o hello.o
root@LAPTOP-PRC71A0C:~/wzy/ch2/hello# gcc hello.o -o hello

运行生成的可执行文件:

root@LAPTOP-PRC71A0C:~/wzy/ch2/hello# ./hello
hello 20221417 //举例这是我的学号

使用git记录过程
初始化git仓库并提交代码:
命令

git init 
git add hello.c 
git commit -m "Add hello.c" 

结果

root@LAPTOP-PRC71A0C:~/wzy/ch2/hello# git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint:   git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint:   git branch -m <name>
Initialized empty Git repository in /root/wzy/ch2/hello/.git/
root@LAPTOP-PRC71A0C:~/wzy/ch2/hello# git add hello.c
root@LAPTOP-PRC71A0C:~/wzy/ch2/hello# git commit -m "Add hello.c"
[master (root-commit) 7f7e182] Add hello.c
 Committer: root <root@LAPTOP-PRC71A0C>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 6 insertions(+)
 create mode 100644 hello.c

2. https://gitee.com/下载课程代码,编译运⾏⽼师课上给出⼤端⼩端的代码,给出运⾏结果。(5分)

$ pwd
bestidiocs2024/ch02/big-little-endian
$ ls
cebitwise.c cepointer.c
cebitfields.c celibrary.c ceunion.c
root@LAPTOP-PRC71A0C:~/bestidiocs2024/ch02/big-little-endian# gcc cebitwise.c -o cebitwise
root@LAPTOP-PRC71A0C:~/bestidiocs2024/ch02/big-little-endian# ./cebitwise
Little Endian (Bitwise Method)

3. 新建⽬录,拷⻉要求2中代码,把代码中的 main 函数都去掉,编写ble.h(包含五个判断函数), main.c 通过命令⾏参数调⽤,⽐如 ./main 1 调⽤cebitwise.c 中的功能。编译运⾏代码,给出运⾏结果。使⽤ git 记录过程(5分)

新建目录并拷贝代码

root@LAPTOP-PRC71A0C:~/wzy/ch2# mkdir blendian
root@LAPTOP-PRC71A0C:~/wzy/ch2# cp ~/bestidiocs2024/ch02/big-little-endian/*.c ~/wzy/ch2/blendian/
root@LAPTOP-PRC71A0C:~/wzy/ch2# cd blendian

修改代码
去掉所有main函数,编写ble.h和main.c。

编写ble.h

// ble.h
#ifndef BLE_H
#define BLE_H

void check_endian_bitwise();
void check_endian_pointer();
void check_endian_bit_field();
void check_endian_standard();
void check_endian_union();

#endif

编写main.c

//main.c
#include "ble.h"

int main(int argc, char *argv[]) {
    if (argc < 2) {
        printf("Usage: ./main <function_number>\n");
        return 1;
    }
    int func_num = atoi(argv[1]);
    switch(func_num) {
        case 1:
            check_endian_bitwise();
            break;
        case 2:
            check_endian_pointer();
            break;
        case 3:
            check_endian_bit_field();
            break;
        case 4:
            heck_endian_standard();
            break;
        case 5:
            check_endian_union();
            break;
        default:
            printf("Invalid function number\n");
    }
    return 0;
}
                                               

编译并运行

root@LAPTOP-PRC71A0C:~/wzy/ch2/blendian# gcc -c *.c
root@LAPTOP-PRC71A0C:~/wzy/ch2/blendian# gcc main.o cebitwise.o cepointer.o cebitfields.o celibrary.o ceunion.o -o main
root@LAPTOP-PRC71A0C:~/wzy/ch2/blendian# ./main 1
Little Endian (Bitwise Method)

记录运行结果

root@LAPTOP-PRC71A0C:~/wzy/ch2/blendian# git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint:   git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint:   git branch -m <name>
Initialized empty Git repository in /root/wzy/ch2/blendian/.git/
root@LAPTOP-PRC71A0C:~/wzy/ch2/blendian# git add main.c
root@LAPTOP-PRC71A0C:~/wzy/ch2/blendian# git commit -m "Add main.c"
[master (root-commit) 46b52ce] Add main.c
 Committer: root <root@LAPTOP-PRC71A0C>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 32 insertions(+)
 create mode 100644 main.c

4. 新建⽬录,拷⻉要求3中代码,制作静态库 libble.a,并调⽤ libble.a,编译运⾏代码,给出运⾏结果。使⽤ git 记录过程(5分)

root@LAPTOP-PRC71A0C:~/wzy/ch2#  cp ~/wzy/ch2/blendian/*.c static_lib/
root@LAPTOP-PRC71A0C:~/wzy/ch2# ls
blendian  hello  main  static_lib
root@LAPTOP-PRC71A0C:~/wzy/ch2# cd static_lib
root@LAPTOP-PRC71A0C:~/wzy/ch2/static_lib# gcc -c *.c
main.c:4:10: fatal error: ble.h: No such file or directory
    4 | #include "ble.h"
      |          ^~~~~~~
compilation terminated.
root@LAPTOP-PRC71A0C:~/wzy/ch2/static_lib# cp ~/wzy/ch2/blendian/ble.h static_lib/
cp: cannot create regular file 'static_lib/': Not a directory
root@LAPTOP-PRC71A0C:~/wzy/ch2/static_lib# cp ~/wzy/ch2/blendian/ble.h ~/wzy/ch2/static_lib
root@LAPTOP-PRC71A0C:~/wzy/ch2/static_lib# gcc -c *.c
root@LAPTOP-PRC71A0C:~/wzy/ch2/static_lib# ar rcs libble.a *.o
root@LAPTOP-PRC71A0C:~/wzy/ch2/static_lib# gcc main.c -L. -lble -o main
root@LAPTOP-PRC71A0C:~/wzy/ch2/static_lib# ./main 1
Little Endian (Bitwise Method)

记录

root@LAPTOP-PRC71A0C:~/wzy/ch2/static_lib# git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint:   git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint:   git branch -m <name>
Initialized empty Git repository in /root/wzy/ch2/static_lib/.git/
root@LAPTOP-PRC71A0C:~/wzy/ch2/static_lib# git add libble.a
root@LAPTOP-PRC71A0C:~/wzy/ch2/static_lib# git commit -m "Add libble.a"
[master (root-commit) 56847ff] Add libble.a
 Committer: root <root@LAPTOP-PRC71A0C>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 libble.a

5. 新建⽬录,拷⻉要求3中代码,制作静态库 libble.a,并调⽤ libble.a,编译运⾏代码,给出运⾏结果。使⽤git 记录过程(5分)

root@LAPTOP-PRC71A0C:~/wzy/ch2# mkdir dynamic_lib
root@LAPTOP-PRC71A0C:~/wzy/ch2#  cp ~/wzy/ch2/blendian/*.c dynamic_lib/
root@LAPTOP-PRC71A0C:~/wzy/ch2# cp ~/wzy/ch2/blendian/ble.h dynamic_lib/
root@LAPTOP-PRC71A0C:~/wzy/ch2# cd dynamic_lib
root@LAPTOP-PRC71A0C:~/wzy/ch2/dynamic_lib# gcc -c -fPIC *.c
root@LAPTOP-PRC71A0C:~/wzy/ch2/dynamic_lib# gcc -shared -o libble.so  *.o
root@LAPTOP-PRC71A0C:~/wzy/ch2/dynamic_lib# gcc main.c -L. -lble -o main
root@LAPTOP-PRC71A0C:~/wzy/ch2/dynamic_lib# export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.
root@LAPTOP-PRC71A0C:~/wzy/ch2/dynamic_lib# ./main 1
Little Endian (Bitwise Method)

记录

root@LAPTOP-PRC71A0C:~/wzy/ch2/dynamic_lib# git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint:   git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint:   git branch -m <name>
Initialized empty Git repository in /root/wzy/ch2/dynamic_lib/.git/
root@LAPTOP-PRC71A0C:~/wzy/ch2/dynamic_lib# ls
ble.h          cebitfields.o  cebitwise.o  celibrary.o  cepointer.o  ceunion.o  main    main.o
cebitfields.c  cebitwise.c    celibrary.c  cepointer.c  ceunion.c    libble.so  main.c
root@LAPTOP-PRC71A0C:~/wzy/ch2/dynamic_lib# git add libble.so
root@LAPTOP-PRC71A0C:~/wzy/ch2/dynamic_lib# git commit -m "Add libble.so"
[master (root-commit) a2085a2] Add libble.so
 Committer: root <root@LAPTOP-PRC71A0C>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100755 libble.so

6. 新建⽬录,按下⾯⽬录拷⻉要求3中代码,编写 Makefile 编译运⾏代码,给出运⾏结果。使⽤ git 记录过程(4分)

tree
.
├── Makefile
├── bin:可执⾏⽂件
├── docs:⽂档
├── include:头⽂件
├── lib:动态库,静态库
└── src:源代码
root@LAPTOP-PRC71A0C:~/wzy/ch2# mkdir makefile_project
root@LAPTOP-PRC71A0C:~/wzy/ch2# cp ~/wzy/ch2/blendian/*.c ~/wzy/ch2/makefile_project
root@LAPTOP-PRC71A0C:~/wzy/ch2# cd makefile_project
root@LAPTOP-PRC71A0C:~/wzy/ch2/makefile_project# cp ~/wzy/ch2/blendian/ble.h ~/wzy/ch2/makefile_project
root@LAPTOP-PRC71A0C:~/wzy/ch2/makefile_project# mkdir -p bin docs include lib src
root@LAPTOP-PRC71A0C:~/wzy/ch2/makefile_project# mv *.c src/
root@LAPTOP-PRC71A0C:~/wzy/ch2/makefile_project# mv *.h include/
root@LAPTOP-PRC71A0C:~/wzy/ch2/makefile_project# ls
bin  docs  include  lib  src
root@LAPTOP-PRC71A0C:~/wzy/ch2/makefile_project# vim makefile
# Makefile
CC = gcc
CFLAGS = -Iinclude
LDFLAGS = -Llib
LDLIBS = -lble

all: bin/main

bin/main: src/main.o lib/libble.a
	$(CC) $(LDFLAGS) -o $@ $< $(LDLIBS)

lib/libble.a: src/*.o
	ar rcs $@ $^

src/%.o: src/%.c include/ble.h
	$(CC) $(CFLAGS) -c -o $@ $<

clean:
	rm -f src/*.o lib/*.a bin/*

但是出现了报错

root@LAPTOP-PRC71A0C:~/wzy/ch2/makefile_project# make
gcc -Iinclude -c -o src/main.o src/main.c
gcc -Iinclude -c -o src/*.o src/cebitfields.c
ar rcs lib/libble.a src/*.o
gcc -Llib -o bin/main src/main.o -lble
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x1b): undefined reference to `main'
collect2: error: ld returned 1 exit status
make: *** [makefile:10: bin/main] Error 1

但是经过我一同操作猛如虎,不知道为什么又成功了

root@LAPTOP-PRC71A0C:~/wzy/ch2/makefile_project# make
gcc -Iinclude -c -o src/main.o src/main.c
gcc -Iinclude -c -o src/cebitfields.o src/cebitfields.c
gcc -Iinclude -c -o src/cebitwise.o src/cebitwise.c
gcc -Iinclude -c -o src/celibrary.o src/celibrary.c
gcc -Iinclude -c -o src/cepointer.o src/cepointer.c
gcc -Iinclude -c -o src/ceunion.o src/ceunion.c
ar rcs lib/libble.a src/cebitfields.o src/cebitwise.o src/celibrary.o src/cepointer.o src/ceunion.o src/main.o
gcc -Llib -o bin/main src/main.o -lble
root@LAPTOP-PRC71A0C:~/wzy/ch2/makefile_project# cd bin
root@LAPTOP-PRC71A0C:~/wzy/ch2/makefile_project/bin# ls
main

😏

7. 提交 git log 结果。

因为是不同文件夹,所以我会转到对应文件夹分开提交

root@LAPTOP-PRC71A0C:~/bestidiocs2024/ch02/c# git log
commit db5f20ac89e483713efd6fad007972c98286aab1 (HEAD -> master, origin/master, origin/HEAD)
Author: rocedu <1301597@qq.com>
Date:   Tue Sep 24 10:38:35 2024 +0800

    add hello.c

commit 98e2c42ec495ad1562e055f7518c74f06443a0c0
Author: rocedu <1301597@qq.com>
Date:   Tue Sep 24 09:22:56 2024 +0800

    argv argc getopt test

commit c06ff9cae7cf2dd98e712414000046a2bcb33662
Author: rocedu <1301597@qq.com>
Date:   Tue Sep 24 08:48:42 2024 +0800

    check endian by c standard library. linux ok

commit 9f70bacf804e2d8c9fffc0f418a3b42fc7c9cbde
Author: rocedu <1301597@qq.com>
Date:   Tue Sep 24 08:46:41 2024 +0800

    check endian by bitfields

commit 8ec71b9cb1a4f03f4786b7bccb43b1507c650a7c
Author: rocedu <1301597@qq.com>
Date:   Tue Sep 24 08:34:06 2024 +0800

    endian check by bits shift
:
root@LAPTOP-PRC71A0C:~/wzy/ch2/main# git log
commit 283e1f5b3548782ef778befeea24454c8ddfce17 (HEAD -> master)
Author: root <root@LAPTOP-PRC71A0C>
Date:   Tue Oct 8 11:11:18 2024 +0800

    Initial commit with time setting and reading functions
root@LAPTOP-PRC71A0C:~/wzy/ch2/static_lib# git log
commit 56847ffa81b537efbd140889ba2df28ad1c0ff93 (HEAD -> master)
Author: root <root@LAPTOP-PRC71A0C>
Date:   Tue Oct 8 12:17:53 2024 +0800

    Add libble.a
root@LAPTOP-PRC71A0C:~/wzy/ch2/static_lib#
root@LAPTOP-PRC71A0C:~/wzy/ch2/dynamic_lib# git log
commit a2085a271e12006cabd2a0fcffb66b48067e2556 (HEAD -> master)
Author: root <root@LAPTOP-PRC71A0C>
Date:   Tue Oct 8 12:24:14 2024 +0800

    Add libble.so
root@LAPTOP-PRC71A0C:~/wzy/ch2/makefile_project# git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint:   git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint:   git branch -m <name>
Initialized empty Git repository in /root/wzy/ch2/makefile_project/.git/
root@LAPTOP-PRC71A0C:~/wzy/ch2/makefile_project# git add makefile
root@LAPTOP-PRC71A0C:~/wzy/ch2/makefile_project# git commit  -m "Add makefile"
[master (root-commit) f834652] Add makefile
 Committer: root <root@LAPTOP-PRC71A0C>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 19 insertions(+)
 create mode 100644 makefile
root@LAPTOP-PRC71A0C:~/wzy/ch2/makefile_project# git log
commit f834652b0fb5b9b01d2921e2912eec3cff4fc99c (HEAD -> master)
Author: root <root@LAPTOP-PRC71A0C>
Date:   Tue Oct 8 12:50:17 2024 +0800

    Add makefile

作业提交要求 (1’)

  1. 记录实践过程和 AI 问答过程,尽量不要截图,给出⽂本内容
  2. (选做)推荐所有作业托管到 gitee或 github 上
  3. (必做)提交作业 markdown⽂档,命名为“学号-姓名-作业题⽬.md”
  4. (必做)提交作业 markdown⽂档转成的 PDF ⽂件,命名为“学号-姓名-作业题⽬.pdf”
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值