Part D — File Permissions (Basics)
Use ls -l
to see permissions. Example:
$ ls -l readme.txt
-rw-r--r-- 1 YOUR_USER YOUR_GROUP 37 Aug 27 10:05 readme.txt
The left string (-rw-r--r--
) is permissions: owner/group/others. Common changes with chmod
:
Command | Effect |
chmod u+x run.sh | Add execute for you (user) |
chmod go-rw secret.txt | Remove read/write for group/others |
chmod 644 file.txt | Numeric mode: rw-r--r-- |
Tip: Directories need the x
bit to be entered (cd
) and the r
bit to list contents.
Part E — From C++ to C, Makefiles, and CLI
1) Start with a simple C++ program
Create hello.cpp
that prints a greeting and sums numbers.
// hello.cpp
#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
cout << "Hello from C++!" << endl;
int sum = 0;
for (int i = 1; i < argc; ++i) sum += stoi(argv[i]);
cout << "Sum: " << sum << endl;
return 0;
}
Compile and run on Odin:
$ g++ -std=c++17 -Wall -Wextra -O2 -o hello_cpp hello.cpp
$ ./hello_cpp 3 4 5
Hello from C++!
Sum: 12
2) Refactor to C
Create hello.c
(use stdio/stdlib and atoi
):
/* hello.c */
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[]) {
printf("Hello from C!\n");
int sum = 0;
for (int i = 1; i < argc; ++i) sum += atoi(argv[i]);
printf("Sum: %d\n", sum);
return 0;
}
Compile and run:
$ gcc -std=c17 -Wall -Wextra -O2 -o hello_c hello.c
$ ./hello_c 3 4 5
Hello from C!
Sum: 12
3) Create a Makefile
In the same directory, make a file named Makefile
:
# Makefile
CXX := g++
CC := gcc
CXXFLAGS := -std=c++17 -Wall -Wextra -O2
CFLAGS := -std=c17 -Wall -Wextra -O2
all: hello_cpp hello_c
hello_cpp: hello.cpp
$(CXX) $(CXXFLAGS) -o $@ $<
hello_c: hello.c
$(CC) $(CFLAGS) -o $@ $<
run-cpp: hello_cpp
./hello_cpp 1 2 3
run-c: hello_c
./hello_c 1 2 3
clean:
rm -f hello_cpp hello_c
Use it like this:
$ make # builds both
$ make run-c # runs C program with sample args
$ make clean # removes binaries
4) Deliverables
readme.txt
updated with a five notes you learned (include commands and explanation of what the command does).
- Your directory structure:
~/cs3600/lab0/
containing hello.cpp
, hello.c
, Makefile
.
- Screenshot or copy/paste of a successful compile & run on Odin.