SDL2 linker problems, MinGW translates wrong path from cmake - c++

I've been trying to add SDL2 library to a program, but I keep getting this error:
'SDL.h' file not found
Here's the cmake file:
cmake_minimum_required(VERSION 3.22)
project(test)
list(APPEND CMAKE_PREFIX_PATH "C:/Users/Windows 10/CLionProjects/test/SDL2/i686-w64-
mingw32/SDL2/i686-w64-mingw32/include/SDL2")
find_package(SDL2 REQUIRED)
set(CMAKE_CXX_STANDARD 14)
add_executable(test main.cpp)
target_include_directories(test PRIVATE ${SDL2_INCLUDE_DIRS})
target_link_libraries(test PRIVATE ${SDL2_LIBRARIES})
message(STATUS IncludeDir=${SDL2_INCLUDE_DIRS} )
message(STATUS IncludeLib=${SDL2_LIBRARIES})
The cmake output after regen is:
-- IncludeDir=/opt/local/i686-w64-mingw32/include/SDL2
-- IncludeLib=-L/opt/local/i686-w64-mingw32/lib -lmingw32 -lSDL2main -lSDL2 -mwindows
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/Windows 10/CLionProjects/test/cmake-build-debug
Here is a picture of the project's structure, and a picture of the include directory, the SDL2 directory entirely is included in the project files.
The include folder has all the header files and contain SDL.h which makes this odd because the compiler should be able to find it.
I'm using the CLion IDE and MinGW compiler.
This project is a test project to add SDL2 and make it work, thus I only have the main class and it contains this:
#include "SDL.h"
int main() {
return 0;
}
Edit: I changed the include to give the full path to SDL.h so main is:
#include "SDL2/i686-w64-mingw32/include/SDL2/SDL.h"
int main() {
return 0;
}
So the file is found but when I run it the following error appears:
[1/2] Building CXX object CMakeFiles/test.dir/main.cpp.obj
[2/2] Linking CXX executable test.exe
FAILED: test.exe
cmd.exe /C "cd . && "D:\Clion\CLion 2022.1\bin\mingw\bin\g++.exe" -g
CMakeFiles/test.dir/main.cpp.obj -o test.exe -Wl,--out-implib,libtest.dll.a
-Wl,--major-image-version,0,--minor-image-version,0 -L/opt/local/i686-w64-mingw32/lib -lmingw32 -lSDL2main -lSDL2 -mwindows -lmingw32 -lSDL2main -lSDL2 -mwindows -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
D:\Clion\CLion 2022.1\bin\mingw\bin/ld.exe: cannot find -lSDL2main
D:\Clion\CLion 2022.1\bin\mingw\bin/ld.exe: cannot find -lSDL2
D:\Clion\CLion 2022.1\bin\mingw\bin/ld.exe: cannot find -lSDL2main
D:\Clion\CLion 2022.1\bin\mingw\bin/ld.exe: cannot find -lSDL2
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
Second Edit:
After changing CMAKE_VERBOSE_MAKEFILE, the build log again is:
"D:\Clion\CLion 2022.1\bin\cmake\win\bin\cmake.exe" --build "C:\Users\Windows 10\CLionProjects\test\cmake-build-debug" --target test
[1/2] "D:\Clion\CLion 2022.1\bin\mingw\bin\g++.exe" -I/opt/local/i686-w64-mingw32/include/SDL2 -g -std=gnu++14 -MD -MT CMakeFiles/test.dir/main.cpp.obj -MF CMakeFiles\test.dir\main.cpp.obj.d -o CMakeFiles/test.dir/main.cpp.obj -c "C:/Users/Windows 10/CLionProjects/test/main.cpp"
[2/2] cmd.exe /C "cd . && "D:\Clion\CLion 2022.1\bin\mingw\bin\g++.exe" -g CMakeFiles/test.dir/main.cpp.obj -o test.exe -Wl,--out-implib,libtest.dll.a -Wl,--major-image-version,0,--minor-image-version,0 -L/opt/local/i686-w64-mingw32/lib -lmingw32 -lSDL2main -lSDL2 -mwindows -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
FAILED: test.exe
cmd.exe /C "cd . && "D:\Clion\CLion 2022.1\bin\mingw\bin\g++.exe" -g CMakeFiles/test.dir/main.cpp.obj -o test.exe -Wl,--out-implib,libtest.dll.a -Wl,--major-image-version,0,--minor-image-version,0 -L/opt/local/i686-w64-mingw32/lib -lmingw32 -lSDL2main -lSDL2 -mwindows -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
D:\Clion\CLion 2022.1\bin\mingw\bin/ld.exe: cannot find -lSDL2main
D:\Clion\CLion 2022.1\bin\mingw\bin/ld.exe: cannot find -lSDL2
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
Changing back to include "SDL.h" only and running on the same cmake config gives this:
"D:\Clion\CLion 2022.1\bin\cmake\win\bin\cmake.exe" --build
"C:\Users\Windows 10\CLionProjects\test\cmake-build-debug" --target test
[1/2] "D:\Clion\CLion 2022.1\bin\mingw\bin\g++.exe" -I/opt/local/i686-w64-mingw32/include/SDL2 -g -std=gnu++14 -MD -MT CMakeFiles/test.dir/main.cpp.obj -MF CMakeFiles\test.dir\main.cpp.obj.d -o CMakeFiles/test.dir/main.cpp.obj -c "C:/Users/Windows 10/CLionProjects/test/main.cpp"
FAILED: CMakeFiles/test.dir/main.cpp.obj
"D:\Clion\CLion 2022.1\bin\mingw\bin\g++.exe" -I/opt/local/i686-w64-mingw32/include/SDL2 -g -std=gnu++14 -MD -MT CMakeFiles/test.dir/main.cpp.obj -MF CMakeFiles\test.dir\main.cpp.obj.d -o CMakeFiles/test.dir/main.cpp.obj -c "C:/Users/Windows 10/CLionProjects/test/main.cpp"
C:/Users/Windows 10/CLionProjects/test/main.cpp:1:10: fatal error: SDL.h: No such file or directory
1 | #include "SDL.h"
| ^~~~~~~
compilation terminated.
ninja: build stopped: subcommand failed.
Like what #Someprogrammerdude said the include directory is being set as /opt/local/i686-w64-mingw32/include/SDL2 instead of the actual path C:/Users/Windows 10/CLionProjects/test/SDL2/i686-w64-mingw32/SDL2/i686-w64-mingw32/include/SDL2

Related

Build simple programm with freetype lib on windows via cmake

I'm trying to build a simple application
using the freetype library on Windows 64.
Freetype lib was compiled from src on windows 64
simple programm
#include <ft2build.h>
#include FT_FREETYPE_H
int main (int argc, char** argv) {
FT_Library ft;
if (FT_Init_FreeType(&ft) != 0) {
//err
}
}
Added environment variable FREETYPE_DIR
and cmake script
cmake_minimum_required(VERSION 3.22)
cmake_path(CONVERT $ENV{COMPILER} TO_CMAKE_PATH_LIST COMPILER)
set(CMAKE_CXX_COMPILER "${COMPILER}/clang++.exe")
set(CMAKE_C_COMPILER "${COMPILER}/clang.exe")
set(CMAKE_RC_COMPILER "${COMPILER}/llvm-rc.exe")
set(CMAKE_BUILD_TYPE Release)
set(EXECUTABLE_OUTPUT_PATH "../bin")
project (text)
file(GLOB SRC
"./src/main.cpp"
)
find_package(Freetype MODULE REQUIRED)
add_executable (${PROJECT_NAME} ${SRC})
target_include_directories(${PROJECT_NAME}
PRIVATE "./include"
PRIVATE ${FREETYPE_INCLUDE_DIRS}
)
target_link_libraries(${PROJECT_NAME}
Freetype::Freetype
)
but I'm stuck, get a linker error, although freetype library is linked
ninja: Entering directory `./build'
[1/1] cmd.exe /C "cd . &&
C:\PROGRA~2\MICROS~2\2022\BUILDT~1\VC\Tools\Llvm\x64\bin\CLANG_~1.EXE -fuse-ld=lld-link
-nostartfiles -nostdlib -O3 -DNDEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrt -
Xlinker /subsystem:console CMakeFiles/text.dir/src/main.cpp.obj -o
D:\git\cpp\opengl\drawtext\bin\text.exe -Xlinker /MANIFEST:EMBED -Xlinker
/implib:D:\git\cpp\opengl\drawtext\bin\text.lib -Xlinker
/pdb:D:\git\cpp\opengl\drawtext\bin\text.pdb -Xlinker /version:0.0 D:/source/freetype-
2.12.1/build/x86_64/Release/lib/freetype.lib -lkernel32 -luser32 -lgdi32 -lwinspool -
lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 -loldnames && cd ."
FAILED: D:/git/cpp/opengl/drawtext/bin/text.exe
cmd.exe /C "cd . &&
C:\PROGRA~2\MICROS~2\2022\BUILDT~1\VC\Tools\Llvm\x64\bin\CLANG_~1.EXE -fuse-ld=lld-link
-nostartfiles -nostdlib -O3 -DNDEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrt -
Xlinker /subsystem:console CMakeFiles/text.dir/src/main.cpp.obj -o
D:\git\cpp\opengl\drawtext\bin\text.exe -Xlinker /MANIFEST:EMBED -Xlinker
/implib:D:\git\cpp\opengl\drawtext\bin\text.lib -Xlinker
/pdb:D:\git\cpp\opengl\drawtext\bin\text.pdb -Xlinker /version:0.0 D:/source/freetype-
2.12.1/build/x86_64/Release/lib/freetype.lib -lkernel32 -luser32 -lgdi32 -lwinspool -
lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 -loldnames && cd ."
lld-link: error: undefined symbol: FT_Init_FreeType
>>> referenced by CMakeFiles/text.dir/src/main.cpp.obj:(main)
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
Find the workaround, recompile freetype lib via Microsoft cl.exe instead of clang.

GLEW + CLION + CMAKE cannot get compiler /cmake to find ref to __imp_glewInit

I'm trying to set to glew but cant seem to get it to work any tips im using cmake with clion?
here is my cmake file:
cmake_minimum_required(VERSION 3.21)
project(openglAttempt)
set(CMAKE_CXX_STANDARD 14)
include_directories(${PROJECT_SOURCE_DIR}/GPU/include)
include_directories(GLEW/include)
link_directories(${PROJECT_SOURCE_DIR}/GPU/lib-mingw-w64)
link_directories(GLEW/lib/Release/Win32)
add_compile_definitions(glew_Static)
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} glfw3 opengl32 gdi32)
and here is the error message:
FAILED: openglAttempt.exe
cmd.exe /C "cd . && C:\PROGRA~1\JETBRA~1\CLION2~1.2\bin\mingw\bin\G__~1.EXE -g CMakeFiles/openglAttempt.dir/main.cpp.obj -o openglAttempt.exe -Wl,--out-implib,libopenglAttempt.dll.a -Wl,--major-image-version,0,--minor-image-version,0 -LC:/Users/nathan/CLionProjects/openglAttempt/GPU/lib-mingw-w64 -LC:/Users/nathan/CLionProjects/openglAttempt/GLEW/lib/Release/Win32 -lglfw3 -lopengl32 -lgdi32 -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
C:\Program Files\JetBrains\CLion 2021.3.2\bin\mingw\bin/ld.exe: CMakeFiles/openglAttempt.dir/main.cpp.obj:C:/Users/nathan/CLionProjects/openglAttempt/main.cpp:26: undefined reference to `__imp_glewInit'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

Build clang on windows with cmake, WinMain reference undefined

i'm trying to build clang with mingw-w64 on windows (10), so i followed this tutorial : https://here-be-braces.com/blog/llvm-clang-on-windows-mingw-revisited
but cmake don't recognise gcc as an C++11 compiler. I run cmake with this command :
cmake C:\llvm -G Ninja -DCMAKE_BUILD_TYPE=Release -DLLVM_BUILD_TOOLS=ON -DLLVM_INCLUDE_EXAMPLES=OFF -DLLVM_BUILD_TESTS=OFF -DLLVM_INCLUDE_TESTS=OFF -DLLVM_ENABLE_EH=ON -DLLVM_ENABLE_RTTI=ON -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DCMAKE_CXX_FLAGS="-IC:/mingw64/x86_64-w64-mingw32/include" -DGCC_INSTALL_PREFIX=%syspath% -DCMAKE_INSTALL_PREFIX=%syspath% -DCMAKE_EXE_LINKER_FLAGS="-lmingw32 -static-libgcc -static-libstdc++ -static -lstdc++ -lm -lpthread -lgcc -lmsvcrt -lmoldname -lgcc_eh -lkernel32 -luser32 -ladvapi32 -liconv -lmingwex" -DBUILD_SHARED_LIBS=OFF -DLLVM_PARALLEL_COMPILE_JOBS=2 -DLLVM_PARALLEL_LINK_JOBS=2 -DCMAKE_INCLUDE_PATH="C:/mingw64/x86_64-w64-mingw32/include" -DGCC_INSTALL_PREFIX="C:/mingw64" -DCMAKE_LIBRARY_PATH="C:/mingw64/x86_64-w64-mingw32/lib"
I obtened the list of librairies with "gcc -v test.c -Wl,--verbose", test.c is just an classic hello world. But with the libmingw32, I get this error :
cmd.exe /C "cd . && C:\mingw64\bin\gcc.exe -lmingw32 -static-libgcc
-static-libstdc++ -static -lstdc++ -lm -lpthread -lgcc -lmsvcrt -lmoldname
-lgcc_eh -lkernel32 -luser32 -ladvapi32 -liconv -lmingwex
CMakeFiles/cmTC_3b639.dir/testCCompiler.c.obj -o cmTC_3b639.exe
-Wl,--out-implib,libcmTC_3b639.dll.a
-Wl,--major-image-version,0,--minor-image-version,0 -lkernel32 -luser32
-lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32
-ladvapi32 && cd ."
CMakeFiles/cmTC_3b639.dir/testCCompiler.c.obj:testCCompiler.c:(.text+0x0):
multiple definition of `main'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/6.2.0/../../../../x86_64-w64-mingw32/lib/../lib\libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x0):
first defined here
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/6.2.0/../../../../x86_64-w64-mingw32/lib/../lib\libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x2e):
undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
Without it, i get further, but i get this : http://pastebin.com/zeMViHzA
CMake Error at projects/libcxx/CMakeLists.txt:423 (message):
C++11 or greater is required but the compiler does not support c++11
How can i get rid of the "WinMain undefined reference" for the cmake tests ?
edit : the gcc version : 6.2.0

Linker fail: member in archive is not an object

I'm trying to build Open CASCADE (6.7.1) library with 64bit mingw on Windows 7 (I am using x86_64-4.9.1-release-posix-seh-rt_v3-rev1 from mingw-w64 project).
When it comes to linking, it fails with an error and I have no idea what could be wrong. The linker command generated by cmake is:
"C:\Program Files (x86)\CMake\bin\cmake.exe" -E remove -f CMakeFiles\TKernel.dir/objects.a
C:\msys64\mingw64\bin\ar.exe cr CMakeFiles\TKernel.dir/objects.a #CMakeFiles\TKernel.dir\objects1.rsp
C:\msys64\mingw64\bin\g++.exe -shared -o ..\out\bin\libTKernel.dll -Wl,--out-implib,..\out\lib\libTKernel.dll.a -Wl,--major-image-version,0,--minor-image-version,0 -Wl,--whole-archive CMakeFiles\TKernel.dir/objects.a -Wl,--no-whole-archive -LC:\msys64\home\user\tcl8.5.16\build -LC:\msys64\home\user\freetype-2.5.3 -ladvapi32 -lgdi32 -luser32 -lkernel32 -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32
And the produced error:
Linking CXX shared library ..\out\bin\libTKernel.dll
CMakeFiles\TKernel.dir/objects.a: member CMakeFiles\TKernel.dir/objects.a(FSD_BinaryFile.cxx.obj) in archive is not an object
collect2.exe: error: ld returned 1 exit status
TKernel\CMakeFiles\TKernel.dir\build.make:11422: recipe for target 'out/bin/libTKernel.dll' failed
This is how the files are compiled:
Building CXX object TKernel/CMakeFiles/TKernel.dir/__/__/src/FSD/FSD_BinaryFile.cxx.obj
cd /d TKernel && C:\msys64\mingw64\bin\g++.exe -DCSFDB -DTKernel_EXPORTS -DWNT -D_OCC64 -D_SCL_SECURE_NO_WARNINGS -MP -Wall -O3 -DNDEBUG -DNo_Exception #CMakeFiles/TKernel.dir/includes_CXX.rsp -MM -D__FSD_DLL -D__MMgt_DLL -D__OSD_DLL -D__Plugin_DLL -D__Quantity_DLL -D__Resource_DLL -D__SortTools_DLL -D__Standard_DLL -D__StdFail_DLL -D__Storage_DLL -D__TColStd_DLL -D__TCollection_DLL -D__TShort_DLL -D__Units_DLL -D__UnitsAPI_DLL -D__IncludeLibrary_DLL -D__Dico_DLL -D__NCollection_DLL -D__Message_DLL -o CMakeFiles\TKernel.dir\__\__\src\FSD\FSD_BinaryFile.cxx.obj -c ..\..\src\FSD\FSD_BinaryFile.cxx
If I check the objects.a file with nm, the contents are .obj, but "File format not recognized" doesn't look good... but don't know what exactly does that mean...
$ nm objects.a
C:\msys64\mingw64\x86_64-w64-mingw32\bin\nm.exe: FSD_BinaryFile.cxx.obj: File format not recognized
C:\msys64\mingw64\x86_64-w64-mingw32\bin\nm.exe: FSD_CmpFile.cxx.obj: File format not recognized
...
Output from file command:
$ file objects.a
objects.a: current ar archive
$ file __/__/src/FSD/FSD_BinaryFile.cxx.obj
__/__/src/FSD/FSD_BinaryFile.cxx.obj: ASCII text, with CRLF line terminators
I also tried to remove the --whole-archive option from the linker command, but that leads to another error:
Linking CXX shared library ..\out\bin\libTKernel.dll
CMakeFiles\TKernel.dir/objects.a: error adding symbols: Archive has no index; run ranlib to add one
collect2.exe: error: ld returned 1 exit status
If I run ranlib on the file, it doesn't help.
Any ideas are appreciated.
edit: Ok, so I guess that the problem is caused by -MM flag, that was used for the build. So it didn't really compile the files, if I understand it correctly.
However I found OCE project, which is some sort of community edition of OCC and it is quite mingw-ready, so I'm gonna use that version and see how it goes...

Can link against a dll using CMake but not qmake

I want to build a simple Qt project on Windows which consists of one main.cpp file and a dll.
I have no problems with building it using CMake:
project(app)
cmake_minimum_required(VERSION 2.6.0)
find_package(Qt4 REQUIRED QtCore QtGui QtXml)
...
add_executable(app main.cpp)
target_link_libraries(app ${QT_LIBRARIES} my_dll.dll)
Then I try to build it using qmake:
...
SOURCES += main.cpp
win32:{
LIBS += -L"my_dll.dll"
}
In this case the build fails and I get linker errors like undefined reference to 'my_function#8', where my_function is a function in my_dll.dll.
When I open my_dll.dll with Dependency Walker I can see that all function names are not decorated. I don't know why qmake tries to resolve function names with '#8' at the end.
This is what CMake writes to the log on linking:
C:\MinGW\bin\g++.exe -g -Wl,--whole-archive CMakeFiles\app.dir/objects.a -Wl,--no-whole-archive -o app.exe -Wl,--out-implib,libapp.dll.a -Wl,--major-image-version,0,--minor-image-version,0 C:\QT\2010.02.1\qt\lib\libQtGuid4.a C:\QT\2010.02.1\qt\lib\libQtXmld4.a C:\QT\2010.02.1\qt\lib\libQtCored4.a -l,my_dll -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32
Also it gives some warnings:
Warning: resolving _my_fucntion#8 by linking to _my_function
Use --enable-stdcall-fixup to disable these warnings
Use --disable-stdcall-fixup to disable these fixups
And this is what qmake says:
g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -mthreads -Wl -o debug/aap1.exe debug/main.o -L'c:/Qt/2010.02.1/qt/lib' -lmingw32 -lqtmaind -Lmy_dll.dll -lQtXmld4 -lQtGuid4 -lQtCored4
How can I build my project using qmake?