make gives an error: first defined here

The error

[ 33%] Building CXX object CMakeFiles/socket.dir/server.cpp.o
[ 66%] Building CXX object CMakeFiles/socket.dir/client.cpp.o
[100%] Linking CXX executable socket
/usr/bin/ld: CMakeFiles/socket.dir/client.cpp.o: in function `error(char const*)':
client.cpp:(.text+0x0): multiple definition of `error(char const*)'; CMakeFiles/socket.dir/server.cpp.o:server.cpp:(.text+0x0): first defined here
/usr/bin/ld: CMakeFiles/socket.dir/client.cpp.o: in function `main':
client.cpp:(.text+0x22): multiple definition of `main'; CMakeFiles/socket.dir/server.cpp.o:server.cpp:(.text+0x22): first defined here
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/socket.dir/build.make:118: socket] Error 1
make[1]: *** [CMakeFiles/Makefile2:95: CMakeFiles/socket.dir/all] Error 2
make: *** [Makefile:103: all] Error 2

The file structure and CMakeLists.txt

The program works when i compile the client.cpp and server.cpp on their own but i want to do it using cmake

I guess you wanted to write something like:

cmake_minimum_required(VERSION 3.18.4)

project(socket)

add_executable(server server.cpp)

add_executable(client client.cpp)
2 Likes

Thanks! that fixed it for me.

May i ask you why did your answer contain 2 add_executeable() functions is it because we wanted to create 2 different executeables?

Exactly

Iā€™m facing the same error. While building the LLVM.
Is there any flag I can pass to cmake so that this error resolves?