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

Time:06-15

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

CodePudding user response:

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

  • Related