Cmake with a simple main and library - cmake

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");}

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.

Related

'Undefined reference to "WinMain#16" ' while setting up SDL2 with CMake

So I want to learn how to use CMake (specifically in CLion), and right now I'm trying to set up SDL2 with it. I got FindSDL2.cmake in my Modules folder, and this is my code:
CMakeLists.txt:
cmake_minimum_required(VERSION 3.13)
project(CLion_Tests)
set(CMAKE_CXX_STANDARD 11)
set(SDL2_PATH "C:/Users/Lerchi/Downloads/SDL/SDL2main/64")
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIR})
add_executable(CLion_Tests main.cpp)
target_link_libraries(CLion_Tests ${SDL2_LIBRARY})
and main.cpp is just this:
#include <iostream>
#include <SDL.h>
int main(int argc, char* args[]) {
SDL_Init(SDL_INIT_EVERYTHING);
std::cout << "Hello, World!" << std::endl;
return 0;
}
The error I'm getting is
CMakeFiles\CLion_Tests.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x1c): undefined reference to `SDL_Init'
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to `WinMain#16'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[2]: *** [CLion_Tests.exe] Error 1
CMakeFiles\CLion_Tests.dir\build.make:87: recipe for target 'CLion_Tests.exe' failed
mingw32-make.exe[1]: *** [CMakeFiles/CLion_Tests.dir/all] Error 2
CMakeFiles\Makefile2:71: recipe for target 'CMakeFiles/CLion_Tests.dir/all' failed
mingw32-make.exe: *** [all] Error 2
Makefile:82: recipe for target 'all' failed

cmake not linking my class I think, mistakes in cmakelists.text likely culprit

I think, I may have improperly set up cmake wrong,(or improperly set up cmake for that matter) or I'm bad and rusty at C++ or both and possibly more. I just want to add a header and separate code into a class that loads images for opengl. I also put them in another directory parallel to other libraries in my linux file tree.
The main project folder has the main cmakelists.text file in it it's called "maficengine". Inside it are also a "build" file a "common" file and "external" file and a file called "Mafic", "maficGuiSourceFiles" file and one called "distrib." I think we can just focus on 2. "mafic" and "external". Inside mafic I have one called mafic.cpp which has my main function and is the entry point. Inside external, there is another folder called "myCustomHeaders".. therein lies a file called "loadTexture.cpp" It contains the loadTexture class declaration and definition. Also inside "myCustomHeaders" there is an "include" file which contains "loadTexture.h"... I cannot seem to instanciate an instance of LoadTexture lt; and call the function of loadtexture(); I expect it to at line 187 in mafic.cpp
#cmakelists.text file from main project folder
# CMake entry point
cmake_minimum_required (VERSION 3.0)
project (maficengine )
find_package(OpenGL REQUIRED)
add_library(loaders
external/myCustomHeaders/loadTexture.cpp
external/myCustomHeaders/include/loadTexture.h
)
set(SOURCES external/myCustomHeaders/loadTexture.cpp )
# Compile external dependencies
add_subdirectory (external)
# On Visual 2005 and above, this module can set the debug working directory
cmake_policy(SET CMP0026 OLD)
cmake_policy(SET CMP0079 NEW)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/external/rpavlik-cmake-modules-fe2273")
include(CreateLaunchers)
include(MSVCMultipleProcessCompile) # /MP
if(INCLUDE_DISTRIB)
add_subdirectory(distrib)
endif(INCLUDE_DISTRIB)
include_directories(
external/AntTweakBar-1.16/include/
external/glfw-3.1.2/include/
external/glm-0.9.7.1/
external/glew-1.13.0/include/
external/assimp-3.0.1270/include/
external/bullet-2.81-rev2613/src/
external/myCustomHeaders/include
common/
)
set(ALL_LIBS
${OPENGL_LIBRARY}
glfw
GLEW_1130
loaders
)
add_definitions(
-DTW_STATIC
-DTW_NO_LIB_PRAGMA
-DTW_NO_DIRECT3D
-DGLEW_STATIC
-D_CRT_SECURE_NO_WARNINGS
)
# Tutorial 17
add_executable(mageengine
Mafic/mafic.cpp
common/shader.cpp
common/shader.hpp
common/controls.cpp
common/controls.hpp
common/texture.cpp
common/texture.hpp
common/objloader.cpp
common/objloader.hpp
common/vboindexer.cpp
common/vboindexer.hpp
common/quaternion_utils.cpp
common/quaternion_utils.hpp
#Mafic/loadTexture.h
#Mafic/loadTexture.cpp
Mafic/StandardShading.vertexshader
Mafic/StandardShading.fragmentshader
)
target_link_libraries(mageengine
${ALL_LIBS}
ANTTWEAKBAR_116_OGLCORE_GLFW
loaders
)
here's mafic.cpp
#include <loadTexture.h>
int main( void )
{
loadTexture lt;
lt.loadtexture();
}
here's my loadTexture.cpp
class loadTexture
{
public:
loadTexture();
~loadTexture();
void loadtexture(){
fprintf(stdout, "loadtexture does something, that's a positive result for this test");
}
};
and finally my loadTexture.h
[code]
#pragma once
#include <GL/glew.h>
//GLuint image;
void loadtexture();
[/code]
Hoping it would build with no errors but I get ...
aaron#Zog:~/Desktop/maficengine/build$ make
[ 2%] Built target ANTTWEAKBAR_116_OGLCORE_GLFW
[ 2%] Built target GLEW_1130
[ 2%] Built target loaders
[ 6%] Built target glfw
[ 7%] Building CXX object CMakeFiles/mageengine.dir/Mafic/mafic.cpp.o
/home/aaron/Desktop/maficengine/Mafic/mafic.cpp:1:9: warning: #pragma once in main file
#pragma once
^~~~
/home/aaron/Desktop/maficengine/Mafic/mafic.cpp: In function ‘int main()’:
/home/aaron/Desktop/maficengine/Mafic/mafic.cpp:187:1: error: ‘loadTexture’ was not declared in this scope
loadTexture lt;
^~~~~~~~~~~
/home/aaron/Desktop/maficengine/Mafic/mafic.cpp:187:1: note: suggested alternative: ‘loadtexture’
loadTexture lt;
^~~~~~~~~~~
loadtexture
/home/aaron/Desktop/maficengine/Mafic/mafic.cpp:189:2: error: ‘lt’ was not declared in this scope
lt.loadtexture();
^~
CMakeFiles/mageengine.dir/build.make:62: recipe for target 'CMakeFiles/mageengine.dir/Mafic/mafic.cpp.o' failed
make[2]: *** [CMakeFiles/mageengine.dir/Mafic/mafic.cpp.o] Error 1
CMakeFiles/Makefile2:75: recipe for target 'CMakeFiles/mageengine.dir/all' failed
make[1]: *** [CMakeFiles/mageengine.dir/all] Error 2
Makefile:129: recipe for target 'all' failed
make: *** [all] Error 2
aaron#Zog:~/Desktop/maficengine/build$

Issue with CMake when using glib library

I am writing a bluez C program to read battery service. I am using CMake for building the code.
My Cmake File is :
# CMakeLists file for module-bluez project
cmake_minimum_required(VERSION 3.02)
project (bluez-module)
find_package(PkgConfig REQUIRED)
# Adding dbus library
pkg_check_modules(DBUS REQUIRED dbus-1>= 1.6)
include_directories(${DBUS_INCLUDE_DIRS})
link_directories(${DBUS_LIBRARY_DIRS})
#Adding glib library
pkg_check_modules(GLIB REQUIRED glib-2.0>=2.23)
include_directories(${GLIB_INCLUDE_DIRS})
link_directories(${GLIB_LIBRARY_DIRS})
pkg_check_modules (DBUSGLIB REQUIRED dbus-glib-1)
include_directories(${DBUSGLIB_INCLUDE_DIRS})
link_directories(${DBUSGLIB_LIBRARY_DIRS})
# Adding bluetooth using extra libs
list(APPEND EXTRA_LIBS "bluetooth")
# Expose 'gattlib.h' to all sub-directories
include_directories(include)
add_executable(bluez-module scantest.c)
# Linking libraries
message(${DBUSGLIB_LIBRARIES})
target_link_libraries(bluez-module ${EXTRA_LIBS})
#target_link_libraries(bluez-module ${DBUS_LIBRARIES})
target_link_libraries(bluez-module ${DBUSGLIB_LIBRARIES})
target_link_libraries(bluez-module ${GLIB_LIBRARIES})
I have to use g_main_loop in my code. But after building the source file I always get the below error :
[ 50%] Linking C executable bluez-module
CMakeFiles/bluez-module.dir/scantest.c.o: In function `read_battery_service':
scantest.c:(.text+0x5b8): undefined reference to `g_dbus_setup_bus'
collect2: error: ld returned 1 exit status
My read_battery function code is as below :
int read_battery_service(struct hci_state *current_hci_state , char *dev_addr)
{
GError *error = NULL;
GDBusClient *client;
GOptionContext *context;
context = g_option_context_new(NULL);
main_loop = g_main_loop_new(NULL, FALSE);
dbus_conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, NULL);
return 0;
}
Just trying to initialize for to access dbus apis.
I have included these headers in the code
#include <assert.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#include <stdio.h>
#include <gdbus.h>
#include <glib/gmain.h>
What would be the issue ? Is glib.h contains the function g_main_loop_new ? Where should I find it ? Or Is CMake not linking glib properly ?
Looks like you are missing the gdbus linker flags. Try using
pkg_check_modules (DBUSGLIB REQUIRED dbus-glib-1) and add
target_link_libraries(module-bluez ${DBUS_LIBRARIES} ${DBUSGLIB_LIBRARIES})
and see if it helps.

Compile an OpenMP program with CMake on OS X

I have a very simple openmp hello world program as this:
#include <stdio.h>
#include <omp.h>
int main()
{
#pragma omp parallel
{
int id = omp_get_thread_num();
printf("hello, from %d.\n", id);
}
return 0;
}
I can compile it in my terminal with gcc-5 -fopenmp hello.c -o hello.o
But I want to code in CLion, it uses CMake to organize project, when I just click the run button, I will got an error
fatal error: 'omp.h' file not found
#include <omp.h>
I did search in google, and add something into my CMakeLists.txt file
This is my CMakeLists.txt file:
cmake_minimum_required(VERSION 3.3)
project(openmp)
OPTION (USE_OpenMP "Use OpenMP" ON)
IF(USE_OpenMP)
FIND_PACKAGE(OpenMP)
IF(OPENMP_FOUND)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
ENDIF()
ENDIF()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fopenmp")
set(SOURCE_FILES hello.c omp.c)
add_executable(openmp ${SOURCE_FILES})
Your code is C, not C++, so instead of changing CMAKE_CXX_FLAGS, change CMAKE_C_FLAGS:
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fopenmp")
And set your C compiler to gcc-5:
set(CMAKE_C_COMPILER /your/path/to/gcc-5)
To know gcc-5 path, in a terminal, type which gcc-5
See also: How to specify new gcc path for cmake

Cmake test program cannot find -lgcc_s

I'm using cmake with custom GCC(with shared libraries) and during cmake compiler test I get following error:
The C compiler "/path/to/gcc/bin/gcc" is not able to compile a simple test program.
...
/path/to/gcc/x86_64-unknown-linux-gnu/bin/ld:
cannot find -lgcc_s
Here's a simple "testme.cpp" file:
#include <iostream>
int main( int argc, char * argv[] ) {
std::cout << "Hello world" << std::endl;
return 0;
}
and here's a CMakeLists.txt:
cmake_minimum_required(VERSION 2.6.2)
project(testme)
file( GLOB srcs "testme.cpp" )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 -Wall -std=c++11")
set(CMAKE_LDFLAGS "${CMAKE_LDFLAGS} -L/path/to/gcc/lib/gcc/x86_64-unknown-linux-gnu/lib64")
add_executable(testme ${srcs})
to build with my custom GCC I'm exporting CXX and CC:
export CXX=/path/to/gcc/bin/g++
export CC=/path/to/gcc/bin/gcc
and then hit:
cmake .
make
and the result is that it can't find libgcc_s.so which is located in the "lib/gcc/x86_64-unknown-linux-gnu/lib64" folder.
However, when I invoke gcc like this:
/path/to/gcc/bin/g++ testme.cpp -L/path/to/gcc/lib/gcc/x86_64-unknown-linux-gnu/lib64
it compiles successfully. I've tried to add it to LD_LIBRARY_PATH but that doesn't seem to help.
So, is there a way to pass library path to CMake compiler check?
Chrono Kitsune was sort of right ... /path/to/gcc/x86_64-unknown-linux-gnu/bin/ld was build with "--enable-shared" and libraries where not in systems search path.