user101
user101

Reputation: 505

Compiling a C program and linking

Do I understand it correctly on how to use cmake for linking

cmake_minimum_required(VERSION 3.2)
project(Sdltest)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -lmingw32 -lSDL2main -lSDL2")

set(SOURCE_FILES main.c)
add_executable(Sdltest ${SOURCE_FILES})

I'm trying to link the sdl2 to my project but I get a lot of errors.

like /src/mingw-org-wsl/4.0-dev/src/libcrt/crt/main.c:91: undefined reference to `WinMain@16'

When I use gcc on cmd, it compiles perfectly

gcc main.c -o test -Wall -Werror -lmingw32 -lSDL2main -lSDL2

By the way I use clion 1.0.5

Upvotes: 0

Views: 168

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409136

The problem is the flags, you mix compiler and linker flags. First of all this is problematic because the compiler flags aren't used when linking, and secondly because the GNU linker needs the libraries to come after the main source (or object) file on the command line, just like when you build manually.

You set the libraries by using the target_link_libraries command in your CMakeLists.txt file:

target_link_libraries(Sdltest mingw32 SDL2main SDL2)

Also, you're not building a C++ project, you're building a C project, so the flags variable should be CMAKE_C_FLAGS.

Finally, if you're wondering about the command and arguments that CMake uses when building, try making with VERBOSE set to non-zero, e.g.

make VERBOSE=1

I'm sure it's possible to add that argument to CLion.


PS.

If you want to enable more warnings, there are even more flags you could add, like -Wextra and -pedantic. Yes it might sometime give false positives, especially when including header files from external libraries, but I think it's better to have to many warnings when developing than to few.

Upvotes: 2

Related Questions