Exercise: Fix the Linker Error

Write code to solve the problem.

Question

A beginner writes a small program across multiple files. The program uses a header file to share a global variable, but the code fails during linking with a multiple definition error.

Below is the compilation command.

gcc main.c helper.c -o program

This program typically fails with an error similar to: multiple definition of `count`.

Your task is to:

  • Fix the code so that it compiles and runs correctly.

  • Keep count shared between both source files.

Exercise: Fix the Linker Error

Write code to solve the problem.

Question

A beginner writes a small program across multiple files. The program uses a header file to share a global variable, but the code fails during linking with a multiple definition error.

Below is the compilation command.

gcc main.c helper.c -o program

This program typically fails with an error similar to: multiple definition of `count`.

Your task is to:

  • Fix the code so that it compiles and runs correctly.

  • Keep count shared between both source files.

int count = 10;
Try it yourself