简体   繁体   中英

Getting error syntax error: unexpected "("When building a C program in Docker

I am trying to build a C program inside a Docker container, I just would like to create a binary file and execute it in the container. I receive no error during compilation but when running within my container the binary file created on Linux Alpine I get this error message:

/usr/jjj-app/bin # ./jjj-linux.out 
./jjj-linux.out: line 1: syntax error: unexpected "("
/usr/jjj-app/bin # 

Notes: I am running make build-linux from the host, in my case macOS.

Any ideas how to build this simple program in Linux environment using Docker? I can use Alpine or another.

main.c

#include <stdio.h>

int main() {
   printf("Hello, World!");
   return 0;
}

Makefile

build-linux:
    docker build -t jjj-app .
    docker run --publish 8081:8080 jjj-app

Dockerfile

FROM alpine
RUN apk update
RUN apk add build-base
COPY . /usr/jjj-app
WORKDIR /usr/jjj-app
RUN gcc /usr/jjj-app/src/main.c -o /usr/jjj-app/bin/jjj-linux.out -r

The gcc -r flag is for partial linking. Presumably to do whole program optimization or other linker steps later on.

If you want a finished exectuable you need to finish linking it.

Either by running gcc again gcc /usr/jjj-app/bin/jjj-linux.out -o /usr/jjj-app/bin/jjj-linux.done.out

or just removing the -r

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM