C++编译输出因权限错误无法运行,执行chmod +x program后仍报错
Hey there! Let's walk through what's causing these problems and how to fix them—since you had it working on another machine, the issue is likely tied to environment differences or missing steps in your Makefile.
First: Why the Initial Permission Error?
On your other machine, either your default umask settings allowed the compiled binary to have executable permissions automatically, or your Makefile implicitly handled it via compiler/linker behavior. On your current system, though, the compiled program file didn't get the executable bit set. Common reasons include:
- Your system's
umaskis stricter (e.g.,0022vs0077), which removes executable permissions for others/yourself by default. - The filesystem where you're compiling is mounted with the
noexecflag (common for external drives or network mounts), which blocks executable permissions entirely. - Your Makefile doesn't explicitly set executable permissions after compilation.
Fix the Permission Issue Properly (No Manual chmod Needed)
Instead of running chmod +x program every time, add this step directly to your Makefile to automate it. You have two solid options:
Option 1: Add chmod to your build rule
Update your Makefile's target for program to include the permission set:
CC = gcc CFLAGS = -Wall -Werror program: your_source_files.o $(CC) $(CFLAGS) -o program your_source_files.o chmod +x program # This ensures executable permissions post-compile clean: rm -f program *.o
Option 2: Use the install command (more robust)
The install command is designed for deploying binaries and lets you set permissions in one step:
CC = gcc CFLAGS = -Wall -Werror program: your_source_files.o $(CC) $(CFLAGS) -o program.tmp your_source_files.o install -m 755 program.tmp program # Sets rwxr-xr-x permissions rm program.tmp clean: rm -f program *.o program.tmp
Now: Troubleshooting the New Error After chmod
Since you didn't share the exact error message, let's cover the most common issues and how to diagnose them:
- "Cannot execute binary file: Exec format error": This means your compiled binary is for a different architecture than your current system. Run
file programto check—if it says something like "ARM 64-bit" but you're on an x86_64 machine, you're cross-compiling by accident. Double-check your Makefile for architecture-specific flags (like-m32or--target=arm-linux-gnueabihf). - Missing shared libraries: If you get an error about "cannot open shared object file", run
ldd programto list all required libraries. Look for lines markednot found—you'll need to install those libraries via your system's package manager (e.g.,apt install libfoo-devon Debian/Ubuntu). - Filesystem restrictions: If you're compiling on an NTFS or FAT32 drive, even with
chmod, the system might not recognize executable permissions. Try compiling on a native Linux filesystem (like ext4) instead.
If you can share the exact error message from after running chmod +x program, we can narrow this down even further!
内容的提问来源于stack exchange,提问作者Morpheus

