I'm new in CLion and CMake and try to build my project for Linux. In IDE it's work fine. But after building I try to run it with MakeFile and it do nothing. I think that problem with CMakeList.txt. Can someone check and explain how to correctly build project.
My CMakeList.txt:
cmake_minimum_required(VERSION 3.17)
project(POng2)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_MODULE_PATH "$ENV{HOME}/.local/share/JetBrains/Toolbox/apps/CLion/ch-
0/203.7717.62/bin/cmake/linux/share/cmake-3.17/Modules")
find_package(SDL2 REQUIRED)
find_package(SDL2_image REQUIRED)
find_package(SDL2_ttf REQUIRED)
include_directories(${SDL2_INCLUDE_DIR}
${SDL2_IMAGE_INCLUDE_DIR}
${SDL2_TTF_INCLUDE_DIR})
add_executable(POng2 main.cpp Game.h Game.cpp Paddle.h Paddle.cpp Entity.h Entity.cpp Ball.h
Ball.cpp Board.h Board.cpp Score.h Score.cpp)
target_link_libraries(POng2 ${SDL2_LIBRARY}
${SDL2_IMAGE_LIBRARIES}
${SDL2_TTF_LIBRARIES})
Files after building:
CLion uses CMake for generation build files.
For default CMake uses GodeBlock - Unix Makefile generator to create Makefile which can be built with make.
So if you want to build your app outside CLion you just need to run make in your build directory or you can use CMake to call build tool by run cmake --build . --target POng2.
Related
I have multiple dependencies for a piece of software. To make everything easier, we made CMake build files for these dependencies. For example, we have lo libfoo, so we make a folder called "make", and in there put the file "buildLibFoo.cmake", which looks like this:
include(ExternalProject)
externalproject_add(LIBFOO
URL https://github.com/lib/foo/archive/refs/tags/v1.6.0.tar.gz
URL_MD5 58e4e09322f2d1e417469eb0987f0531b
BUILD_IN_SOURCE 0
PREFIX ${CMAKE_BINARY_DIR}/deps/libfoo
SOURCE_DIR ${CMAKE_BINARY_DIR}/deps/libfoo/src/libfoo
CONFIGURE_COMMAND
cd <SOURCE_DIR> &&
autoreconf -i &&
./configure
BUILD_COMMAND
cd <SOURCE_DIR> &&
make -j$(nproc)
INSTALL_COMMAND ""
)
set(LIBFOO_INCLUDE_PATH ${CMAKE_BINARY_DIR}/deps/libfoo/src/libfoo/utils)
set(LIBFOO_LIB_PATH ${CMAKE_BINARY_DIR}/deps/libfoo/src/libfoo/utils/.libs)
In my primary CMakeFiles.cmake I add the following:
cmake_minimum_required(VERSION 3.18.1)
project("testproject")
set (COMPILE_FLAGS "-O2")
## Need LIBFOO
if(NOT (${LIBFOO_INCLUDE_PATH} AND ${LIBFOO_LIB_PATH}))
include(buildTSS.cmake)
include_directories(${LIBFOO_INCLUDE_PATH})
link_directories(${LIBFOO_LIB_PATH})
list(APPEND DEPENDENCY_LIST "LIBFOO")
endif()
add_library(
testproject
SHARED
testproject.cpp)
target_link_libraries(testproject libfoo)
add_dependencies(testproject ${DEPENDENCY_LIST})
While the compiles fine if I run cmake CMakeLists.txt and make, it fails to do so if I use Android Studio (with native code) or CLion. Here is an example in Android Studio.
Build command failed.
Error while executing process /home/derp/Android/Sdk/cmake/3.18.1/bin/ninja with arguments {-C /home/derp/AndroidStudioProjects/testproject/app/.cxx/Debug/565m494g/arm64-v8a testproject}
ninja: Entering directory `/home/derp/AndroidStudioProjects/testproject/app/.cxx/Debug/565m494g/arm64-v8a'
ninja: error: build.ninja:178: bad $-escape (literal $ must be written as $$)
Any ideas how to make it work through the IDE?
I ran into this same error for an existing project I was trying to load with CLion that also built fine using cmake from the command line.
In my case the solution was to override the default setting CLion uses for the generator tool (ninja) and set it to the "Let CMake decide"
1. bg: i got a project to develop the embedded camera system, the system is using mips 32bit, i got the toolchain in ubuntu. go version: 1.17.7
2. my question:
2.1) pure golang cross compile into mipsle platform is good and working.the command it use is :
GOOS=linux GOARCH=mipsle CGO_ENABLED=0 go build main.go
2.2) but i got some third-party lib which they offer as xxx.so and xxx.h to me. when i finish coding it into my go project using cgo. i want to cross complie into mips system which i found fail.
3. what i had try:
3.1) i know go is not support cross complie cgo, so i try the normal way which is like this:
GOOS=linux GOARCH=mipsle CGO_ENABLED=1 CC=/opt/mips-gcc720-glibc226/bin/mips-linux-gnu-gcc go build main.go
result:
# runtime/cgo
In file included from /opt/mips-gcc720-glibc226/mips-linux-gnu/libc/usr/include/features.h:447:0,
from /opt/mips-gcc720-glibc226/mips-linux-gnu/libc/usr/include/bits/libc-header-start.h:33,
from /opt/mips-gcc720-glibc226/mips-linux-gnu/libc/usr/include/stdlib.h:25,
from _cgo_export.c:3:
/opt/mips-gcc720-glibc226/mips-linux-gnu/libc/usr/include/gnu/stubs.h:11:11: fatal error: gnu/stubs-o32_hard.h: No such file or directory
# include <gnu/stubs-o32_hard.h>
^~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
3.2) i also try gccgo, in ubuntu, i try:
go build -compiler=mipsel-linux-gnu-gccgo main.go
result:
invalid value "mipsel-linux-gnu-gccgo" for flag -compiler: unknown compiler "mipsel-linux-gnu-gccgo"
usage: go build [-o output] [build flags] [packages]
Run 'go help build' for details.
3.3) i had try xgo project to compile, which is can output a result, but when i move to the mipsle system it still can not working.
so , can any body help? if need the toolchain to test, i can send you
cantact me: justforjobonly#126.com
thanks!
I have a basic rust/cargo project with a single main file and some basic dependencies. The cargo build command works fine when the target is not specified (I am using windows so it builds to windows), but when I try to cross compile the program to linux using cargo build --target=x86_64-unknown-linux-gnu or cargo build --target=x86_64-unknown-linux-musl, the process fails with the following error: linker 'cc' not found.
Does anyone have an idea how to get around this? Is there a specific linker I need to install?
Thanks.
I've just figured it out.
It turns out you need to tell cargo to use the LLVM linker instead. You do this by creating a new directory called .cargo in your base directory, and then a new file called config.toml in this directory. Here you can add the lines:
[target.x86_64-unknown-linux-musl]
rustflags = ["-C", "linker-flavor=ld.lld"]
Then building with the command cargo build --target=x86_64-unknown-linux-musl should work!
So I am trying to build and test out a CMake with the Android NDK on Android Studio. I can get my library to compile, but it doesn't seem to want to pull any third-party dependencies over. I've been reading through the toolchain and looking for better documentations, with no luck. Can someone tell me if I am missing?
cmake_minimum_required(VERSION 3.4.1)
set(SFML_PATH ${ANDROID_NDK}/sources/sfml)
set(SFML_LIB_PATH ${SFML_PATH}/lib/${ANDROID_NDK_ABI_NAME})
set(SFML_LIB_SYSTEM ${SFML_LIB_PATH}/libsfml-system.so)
set(SFML_LIB_AUDIO ${SFML_LIB_PATH}/libsfml-audio.so)
set(SFML_LIB_GRAPHICS ${SFML_LIB_PATH}/libsfml-graphics.so)
set(SFML_LIB_NETWORK ${SFML_LIB_PATH}/libsfml-network.so)
set(SFML_LIB_WINDOW ${SFML_LIB_PATH}/libsfml-window.so)
set(SFML_LIB_ACTIVITY ${SFML_LIB_PATH}/libsfml-activity.so)
set(SFML_LIB_MAIN ${SFML_LIB_PATH}/libsfml-main.a)
set(SFML_LIBS ${SFML_LIB_SYSTEM} ${SFML_LIB_GRAPHICS} ${SFML_LIB_AUDIO} ${SFML_LIB_WINDOW} ${SFML_LIB_ACTIVITY})
include_directories(${SFML_PATH}/include)
link_directories(${SFML_LIB_PATH})
add_library(native-lib SHARED
src/main/cpp/native-lib.cpp)
target_link_libraries(native-lib log ${SFML_LIBS})
#file(COPY ${SFML_LIBS} DESTINATION ${__android_install_path})
FOREACH(SFML_LIB ${SFML_LIB})
execute_process( COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${SFML_LIB}" "${LIBRARY_OUTPUT_PATH}/${SFML_LIB}" RESULT_VARIABLE __fileCopyProcess )
MESSAGE("Lib: ${SFML_LIB}")
ENDFOREACH(SFML_LIB)
Above is my CMakeLists.txt. I have done a little hacking to get it to compile with SFML with the paths, as I have not found good documentation with CMake and Android yet.
May you add more info for:
"but it doesn't seem to want to pull any third-party dependencies over."?
this one:
https://github.com/googlesamples/android-ndk/tree/master/hello-libs
has static and shared 3rd party libs, you may try it.
For the shared dependent lib, you will need to pack them into APK, that is done inside gradle, cmake will not do it.
The above example shows that, basically they need to be copied into your app/src/main/jniLibs too so they will be packed into apk, and pushed to your android phone/tablet. At runtime they could be loaded.
I have tried to put a group of libraries into one directory, and use
link_directories(...)
then just put the lib names directly into
target_link_libraries(...)
also works. Make sure you have the right libs for the ABIs you intend to support for your app [looks like you are just building for one ABI].
The process could be little long it will depend on your android skills.
An example could be similar to this process:
Crosscompile sfml.
Create your jni bridge
Generate with cmake the project and compile
Copy your files to android studio. create java loading library code.
I guess that you have crosscompiled sfml and you know how works crosscompiling process, if I am wrong check these link below:
Tutorial:
https://github.com/SFML/SFML/wiki/Tutorial:-Building-SFML-for-Android
Source code:
https://github.com/SFML/SFML
Toolchain:
https://github.com/SFML/SFML/blob/master/cmake/toolchains/android.toolchain.cmake
Changes on your cmake:
add this file
FIND_PACKAGE(SFML required)
In cmake put your SFML build directory and cmake will fills your VARIABLES
automatically for instance this variables:
set(SFML_PATH ${ANDROID_NDK}/sources/sfml)
set(SFML_LIB_PATH ${SFML_PATH}/lib/${ANDROID_NDK_ABI_NAME})
set(SFML_LIB_SYSTEM ${SFML_LIB_PATH}/libsfml-system.so)
set(SFML_LIB_AUDIO ${SFML_LIB_PATH}/libsfml-audio.so)
set(SFML_LIB_GRAPHICS ${SFML_LIB_PATH}/libsfml-graphics.so)
set(SFML_LIB_NETWORK ${SFML_LIB_PATH}/libsfml-network.so)
set(SFML_LIB_WINDOW ${SFML_LIB_PATH}/libsfml-window.so)
set(SFML_LIB_ACTIVITY ${SFML_LIB_PATH}/libsfml-activity.so)
set(SFML_LIB_MAIN ${SFML_LIB_PATH}/libsfml-main.a)
There are two ways to make android studio native apps:
Easy way:
Create JNI bridge:
Crosscompile your cmake script and copy your lib to app/src/main/jniLibs
add library in execution time
code:
try
{
Log.v(LOG_TAG, "adding your library");
System.loadLibrary(your_library);
}
catch(UnsatisfiedLinkError e)
{
Log.e(LOG_TAG,e.getMessage());
}
More complete way (it allows to debug library)
Create your ndk module in gradle
example
android.ndk {
moduleName = "your_library"
cppFlags.add("-fexceptions")
//cppFlags.add("-std=c++11")
//cFlags.add("-fopenmp")
cppFlags.add("-I" + file("src/main/jni").absolutePath)
stl = "gnustl_shared" // Which STL library to use: gnustl or stlport
ldLibs.addAll(["android", "EGL", "GLESv2", "dl", "log", "z"])
String libsDir = curDir.absolutePath + "/src/main/jniLibs/armeabi/"
ldLibs.add(libsDir + "your_native_lib.so")
}
I have used CMake to make a library and then made a test exe. After building, I would like to automatically run my test cases. Here is my CMakeLists.txt. It makes the .exe OK but does not run it. I am using Linux.
cmake_minimum_required (VERSION 2.8.7)
project (tests)
set(LIBRARY_NAME exetests)
set(LIBRARY_SOURCES RunAllTests.cpp Tests.cpp )
set(CMAKE_CXX_FLAGS "-fPIC -Werror -O2 -std=c++0x -g")
add_executable(exetests ${LIBRARY_SOURCES})
target_link_libraries(exetests CppUTest )
target_link_libraries(exetests CppUTestExt )
target_link_libraries(exetests testLibrary )
#THIS IS WRONG. EXE is not Run
add_custom_target( COMMAND ./exetests )
You can use CTest for testing of executables created by CMake. In your CMakeLists.txt, use commands enable_testing and add_test
...
enable_testing()
...
add_executable(exetests ${LIBRARY_SOURCES})
...
add_test(NAME mytest1 COMMAND exetests)
in your binary directory, compile make exetests and run testing by ctest. Some additional information could be found on CMake Wiki.
From the documentation:
The second signature adds a custom command to a target such as a library or executable. This is useful for performing an operation before or after building the target. The command becomes part of the target and will only execute when the target itself is built. If the target is already built, the command will not execute.
add_executable(RunAllTests RunAllTests.cpp)
target_link_libraries(RunAllTests imp_cpputest LedDriverTest LedDriver sprintfTest RuntimeErrorStub)
add_custom_command( TARGET RunAllTests COMMAND cd ../bin && ./RunAllTests POST_BUILD)