0
votes

I have a CMakelist.txt where i add external header files and lib. files for my build. Now when I call custom headers ("myown.h") in my main function I get errors when running CMake linking.

So I found out that I have to add my own headers and .cpp to a library with add_library and then add them under target_link_libraries.

However I get a link error while building as long as I want to call the code from my own header.

Does anyone have an idea where the error is or how I can continue? Here is my CMakelist:

cmake_minimum_required(VERSION 3.0.0)
project(MIELE_OCULUS VERSION 0.1.0)

#Hier weden die include files gefunden
INCLUDE_DIRECTORIES(
    C:/Projekte/Cpp/dev/vcpkg/installed/x64-windows/include
)
#Hier werden die lib files gefunden
LINK_DIRECTORIES(
    C:/Projekte/Cpp/dev/vcpkg/installed/x64-windows/lib,
    C:/Program Files (x86)/Windows Kits/10/Lib/10.0.19041.0/um/x64
    
)
add_library(
    test
    testclass.h
    testclass.cpp
)
add_executable(MIELE_OCULUS 
    main.cpp
)

TARGET_LINK_LIBRARIES(MIELE_OCULUS 
    cpprest_2_10
    WS2_32
    test
)

UPDATE:

With these changes in CMakelist.txt you can build the program, but after changing the src code or cleaning up the CMake project, linking errors will occur again.

changed CMakelist:

cmake_minimum_required(VERSION 3.0.0)
project(MIELE_OCULUS VERSION 0.1.0)

add_library(
    mylib
    includes/mylib/testclass.h
    includes/mylib/testclass.cpp
)

#Hier weden die include files gefunden
TARGET_INCLUDE_DIRECTORIES(mylib PUBLIC 
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/mylib>
    $<INSTALL_INTERFACE:include/mylib>  # <prefix>/include/mylib

)

INCLUDE_DIRECTORIES(
    C:/Projekte/Cpp/dev/vcpkg/installed/x64-windows/include
    C:/Projekte/Cpp/dev/Miele_Oculus
)
#Hier werden die lib files gefunden
LINK_DIRECTORIES(
    C:/Projekte/Cpp/dev/vcpkg/installed/x64-windows/lib
    C:/Program Files (x86)/Windows Kits/10/Lib/10.0.19041.0/um/x64
    
)

add_executable(MIELE_OCULUS 
    main.cpp
)

TARGET_LINK_LIBRARIES(MIELE_OCULUS 
    cpprest_2_10
    WS2_32
    mylib
)

Here is the verbose output:

Der Buildvorgang wurde am 01.11.2020 16:19:04 gestartet. Projekt "C:\Projekte\Cpp\dev\Miele_Oculus\build\ALL_BUILD.vcxproj" auf Knoten "1" (Standardziele). Das Projekt "C:\Projekte\Cpp\dev\Miele_Oculus\build\ALL_BUILD.vcxproj" (1) erstellt "C:\Projekte\Cpp\dev\Miele_Oculus\b uild\ZERO_CHECK.vcxproj" (2) auf Knoten "1" (Standardziele). InitializeBuildStatus: "x64\Debug\ZERO_CHECK\ZERO_CHECK.tlog\unsuccessfulbuild" wird erstellt, da "AlwaysCreate" angegeben wurde. CustomBuild: Alle Ausgaben sind aktuell. FinalizeBuildStatus: Die Datei "x64\Debug\ZERO_CHECK\ZERO_CHECK.tlog\unsuccessfulbuild" wird gelöscht. Aktualisieren des Timestamps von "x64\Debug\ZERO_CHECK\ZERO_CHECK.tlog\ZERO_CHECK.lastbuildstate". Die Erstellung von Projekt "C:\Projekte\Cpp\dev\Miele_Oculus\build\ZERO_CHECK.vcxproj" ist abgeschlossen (Standardziele ).

Das Projekt "C:\Projekte\Cpp\dev\Miele_Oculus\build\ALL_BUILD.vcxproj" (1) erstellt "C:\Projekte\Cpp\dev\Miele_Oculus\b uild\MIELE_OCULUS.vcxproj" (3) auf Knoten "1" (Standardziele). Das Projekt "C:\Projekte\Cpp\dev\Miele_Oculus\build\MIELE_OCULUS.vcxproj" (3) erstellt "C:\Projekte\Cpp\dev\Miele_Oculu s\build\mylib.vcxproj" (4) auf Knoten "1" (Standardziele). InitializeBuildStatus: "mylib.dir\Debug\mylib.tlog\unsuccessfulbuild" wird erstellt, da "AlwaysCreate" angegeben wurde. CustomBuild: Alle Ausgaben sind aktuell. VcpkgTripletSelection: Using triplet "x64-windows" from "C:\Projekte\Cpp\dev\vcpkg\installed\x64-windows" ClCompile: Alle Ausgaben sind aktuell. Lib: Alle Ausgaben sind aktuell. mylib.vcxproj -> C:\Projekte\Cpp\dev\Miele_Oculus\build\Debug\mylib.lib AppLocalFromInstalled: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -noprofile -File "C:\Projekte\Cpp\d ev\vcpkg\scripts\buildsystems\msbuild\applocal.ps1" "C:\Projekte\Cpp\dev\Miele_Oculus\build\Debug\mylib.lib" "C:\Proj ekte\Cpp\dev\vcpkg\installed\x64-windows\debug\bin" "mylib.dir\Debug\mylib.tlog\mylib.write.1u.tlog" "mylib.dir\Debug \vcpkg.applocal.log" FinalizeBuildStatus: Die Datei "mylib.dir\Debug\mylib.tlog\unsuccessfulbuild" wird gelöscht. Aktualisieren des Timestamps von "mylib.dir\Debug\mylib.tlog\mylib.lastbuildstate". Die Erstellung von Projekt "C:\Projekte\Cpp\dev\Miele_Oculus\build\mylib.vcxproj" ist abgeschlossen (Standardziele).

InitializeBuildStatus: Aktualisieren des Timestamps von "MIELE_OCULUS.dir\Debug\MIELE_OCULUS.tlog\unsuccessfulbuild". CustomBuild: Alle Ausgaben sind aktuell. VcpkgTripletSelection: Using triplet "x64-windows" from "C:\Projekte\Cpp\dev\vcpkg\installed\x64-windows" ClCompile: Alle Ausgaben sind aktuell. Link: C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\bin\HostX64\x64\link.exe /ERR ORREPORT:QUEUE /OUT:"C:\Projekte\Cpp\dev\Miele_Oculus\build\Debug\MIELE_OCULUS.exe" /INCREMENTAL /NOLOGO /LIBPATH:"C: /Projekte/Cpp/dev/vcpkg/installed/x64-windows/lib" /LIBPATH:"C:/Projekte/Cpp/dev/vcpkg/installed/x64-windows/lib/Debu g" /LIBPATH:C:/Program /LIBPATH:C:/Program/Debug /LIBPATH:C:/Projekte/Cpp/dev/Miele_Oculus/Files /LIBPATH:C:/Projekte /Cpp/dev/Miele_Oculus/Files/Debug /LIBPATH:"C:/Projekte/Cpp/dev/Miele_Oculus/(" /LIBPATH:"C:/Projekte/Cpp/dev/Miele_O culus/(/Debug" /LIBPATH:C:/Projekte/Cpp/dev/Miele_Oculus/x86 /LIBPATH:C:/Projekte/Cpp/dev/Miele_Oculus/x86/Debug /LIB PATH:"C:/Projekte/Cpp/dev/Miele_Oculus/)" /LIBPATH:"C:/Projekte/Cpp/dev/Miele_Oculus/)/Debug" /LIBPATH:/Windows /LIBP ATH:/Windows/Debug /LIBPATH:C:/Projekte/Cpp/dev/Miele_Oculus/Kits/10/Lib/10.0.19041.0/um/x64 /LIBPATH:C:/Projekte/Cpp /dev/Miele_Oculus/Kits/10/Lib/10.0.19041.0/um/x64/Debug /LIBPATH:"C:\Projekte\Cpp\dev\vcpkg\installed\x64-windows\deb ug\lib" /LIBPATH:"C:\Projekte\Cpp\dev\vcpkg\installed\x64-windows\debug\lib\manual-link" cpprest_2_10.lib WS2_32.lib Debug\mylib.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.l ib advapi32.lib "C:\Projekte\Cpp\dev\vcpkg\installed\x64-windows\debug\lib*.lib" /MANIFEST /MANIFESTUAC:"level='asIn voker' uiAccess='false'" /manifest:embed /DEBUG /PDB:"C:/Projekte/Cpp/dev/Miele_Oculus/build/Debug/MIELE_OCULUS.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:/Projekte/Cpp/dev/Miele_Oculus/build/Debug/MIELE_OCULUS .lib" /MACHINE:X64 /machine:x64 MIELE_OCULUS.dir\Debug\main.obj LINK : fatal error LNK1104: Datei "C:\Projekte\Cpp\dev\Miele_Oculus\build\Debug\MIELE_OCULUS.exe" kann nicht geöffnet w erden. [C:\Projekte\Cpp\dev\Miele_Oculus\build\MIELE_OCULUS.vcxproj] Die Erstellung des Projekts "C:\Projekte\Cpp\dev\Miele_Oculus\build\MIELE_OCULUS.vcxproj" ist abgeschlossen (Standardzi ele) -- FEHLER.

Die Erstellung des Projekts "C:\Projekte\Cpp\dev\Miele_Oculus\build\ALL_BUILD.vcxproj" ist abgeschlossen (Standardziele ) -- FEHLER.

Fehler beim Buildvorgang.

"C:\Projekte\Cpp\dev\Miele_Oculus\build\ALL_BUILD.vcxproj" (Standardziel) (1) -> "C:\Projekte\Cpp\dev\Miele_Oculus\build\MIELE_OCULUS.vcxproj" (Standardziel) (3) -> (Link Ziel) -> LINK : fatal error LNK1104: Datei "C:\Projekte\Cpp\dev\Miele_Oculus\build\Debug\MIELE_OCULUS.exe" kann nicht geöffnet werden. [C:\Projekte\Cpp\dev\Miele_Oculus\build\MIELE_OCULUS.vcxproj]

0 Warnung(en)
1 Fehler

Verstrichene Zeit 00:00:00.90

1
C:/Projekte/Cpp/dev/vcpkg/installed/x64-windows/lib, the comma is wrong - drescherjm
Thanks, I have removed it but unfortunately this does not change my problem :/ - zombieanfuehrer
Adding a header into add_library call doesn't add include directories for the header. You need to add these directories manually, with INCLUDE_DIRECTORIES or TARGET_INCLUDE_DIRECTORIES. - Tsyvarev
OK, thanks now it works but i have a strange cmake behavior. When I build the project the first time cmake creates it and I can run the finished program. If i change the src code and/or clean up the cmake project, I get a link error again. If i restart my IDE (VS CODE) and build it then it works with the changed src code. I will post my changed CMakelist.txt i think here is the prob. - zombieanfuehrer
ustom headers ("myown.h") in my main function I get errors when running CMake linking. Headers don't affect linking. Please post the errors. Please post the verbose output of building - cmake --build dir --verbose - KamilCuk

1 Answers

0
votes

Now it works

1: Adding a header into add_library call doesn't add include directories for the header. You need to add these directories manually, with INCLUDE_DIRECTORIES or TARGET_INCLUDE_DIRECTORIES

I added this part:

#Eigene header Dateien include
TARGET_INCLUDE_DIRECTORIES(mylib PUBLIC 
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/includes/mylib>
$<INSTALL_INTERFACE:includes/mylib>  # <prefix>/includes/mylib

Thanks a lot to Tsyvarev ! :)

2:

The "strange" behavoir was a bad mistake from me, cause I didnt known that i delete the output terminal in windows bevor i start a new build in the command window

Thank you guys!