Issue
i tried a lot of commands and finally give up. I know I did it in past. But please tell me why it doesn't work :,( Its just a main.c with a little library testlib.h
$cmake ..
-- Configuring done
CMake Error at CMakeLists.txt:15 (add_library):
Cannot find source file:
PUBLIC
Tried extensions .c .C .c++ .cc .cpp .cxx .cu .mpp .m .M .mm .ixx .cppm .h
.hh .h++ .hm .hpp .hxx .in .txx .f .F .for .f77 .f90 .f95 .f03 .hip .ispc
CMake Error at CMakeLists.txt:15 (add_library):
No SOURCES given to target: lib
CMakeLists.txt:
cmake_minimum_required(VERSION 3.0)
project(WTF)
add_executable(result main.c)
set_target_properties(result PROPERTIES
C_STANDARD 11
C_STANDARD_REQUIRED ON
C_EXTENSIONS OFF
)
set (DIR "${CMAKE_CURRENT_SOURCE_DIR}")
set (DIRLIBS "${CMAKE_CURRENT_SOURCE_DIR}/libs")
message("${DIRLIBS}")
message("${DIR}")
add_library(lib PUBLIC testlib.h testlib.c)
target_include_directories(result PUBLIC ${DIR} ${DIRLIBS} )
set_target_properties(result PROPERTIES LINKER_LANGUAGE CXX)
target_link_libraries(result PUBLIC lib )
$cmake --build .
usr/bin/ld: cannot find -ltestlib.h
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/result.dir/build.make:97: result] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/result.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
main.c
#include "testlib.h"
#include <stdio.h>
//#include "libs/lib2.h"
int main(void){
printf("hw");
f1();
testlib.h
void f1(void);
testlib.c
#include <stdio.h>
#include "testlib.h"
void f1(void){
printf("f1");}
Solution
Try this CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
project(WTF)
add_executable(result main.c)
set_target_properties(result PROPERTIES
C_STANDARD 11
C_STANDARD_REQUIRED ON
C_EXTENSIONS OFF
)
set (DIR "${CMAKE_CURRENT_SOURCE_DIR}")
set (DIRLIBS "${CMAKE_CURRENT_SOURCE_DIR}/libs")
message("${DIRLIBS}")
message("${DIR}")
add_library(test STATIC testlib.h testlib.c)
target_include_directories(result PUBLIC ${DIR} ${DIRLIBS} )
set_target_properties(result PROPERTIES LINKER_LANGUAGE CXX)
target_link_libraries(result PUBLIC test )
problem is with the syntax of add_library() use [STATIC | SHARED | MODULE] instead of PUBLIC.
Answered By - harry