Cannot link against brew-installde cppunit in OSX travis image - c++

I have a cmake-based C++ project on github that builds with Travis CI. The .travis.yml file has the following for building on OSX with clang:
language: cpp
jobs:
include:
- os: osx
compiler: gcc
osx_image: xcode11.2
env:
- GCC_VER="9"
- MATRIX_EVAL="CC=gcc-${GCC_VER} && CXX=g++-${GCC_VER}"
addons:
homebrew:
packages:
- cppunit
- mruby
... (other OS) ...
before_script:
- eval "${MATRIX_EVAL}"
script:
- cmake -D ENABLE_TESTS:BOOL=TRUE .
- cmake --build . -- -j2
- ctest -j2
CppUnit is installed by brew.
The cmake command runs fine and finds CppUnit in /usr/local (i.e. CPPUNIT_INDLUE_DIR is correctly set to /usr/local/include and CPPUNIT_LIBRARIES is correctly set to /usr/local/lib/libcppunit.dylib. However when building the tests, I get link errors about undefined symbols related to cppunit, indicating that it is not correctly linking against cppunit.
The same project builds fine on my MacBook (also with a brew-installed cppunit), and it correctly builds on Linux with both gcc-9 and with clang, with cppunit installed via apt.
I thought I needed to set DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:/usr/local/lib somewhere, but when I do that I get new errors:
$ cmake -D ENABLE_TESTS:BOOL=TRUE .
dyld: Symbol not found: __cg_jpeg_resync_to_restart
Referenced from: /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO
Expected in: /usr/local/lib/libJPEG.dylib
in /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO
/Users/travis/.travis/functions: line 109: 3565 Abort trap: 6 cmake -D ENABLE_TESTS:BOOL=TRUE .
The command "cmake -D ENABLE_TESTS:BOOL=TRUE ." exited with 134.
Here is the CMakeLists.txt file at the root of the source:
cmake_minimum_required (VERSION 3.1)
project (mrbind17 CXX)
set (CMAKE_CXX_STANDARD 17)
set (CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH}
"${CMAKE_CURRENT_SOURCE_DIR}/cmake")
add_definitions(-g)
option(ENABLE_TESTS "Build tests. May require CppUnit_ROOT" OFF)
include_directories (${CMAKE_CURRENT_SOURCE_DIR}/include)
find_package (Mruby REQUIRED)
include_directories (${Mruby_INCLUDE_DIR})
find_package (CppUnit)
if (CPPUNIT_FOUND)
message(STATUS "CppUnit found, unit tests will be compiled")
message(STATUS "CPPUNIT_INCLUDE_DIR : ${CPPUNIT_INCLUDE_DIR}")
message(STATUS "CPPUNIT_LIBRARIES : ${CPPUNIT_LIBRARIES}")
include_directories(${CPPUNIT_INCLUDE_DIR})
enable_testing()
if(${ENABLE_TESTS})
add_subdirectory (test)
endif(${ENABLE_TESTS})
else (CPPUNIT_FOUND)
message(STATUS "CppUnit not found, unit tests will not be compiled")
endif (CPPUNIT_FOUND)
and the CMakeLists.txt in the test directory:
add_executable(interpreter_test main.cpp interpreter_test.cpp)
target_link_libraries(interpreter_test ${Mruby_LIBRARIES} ${CPPUNIT_LIBRARIES})
add_test(NAME interpreter_test COMMAND ./interpreter_test interpreter_test.xml)
add_executable(function_test main.cpp function_test.cpp)
target_link_libraries(function_test ${Mruby_LIBRARIES} ${CPPUNIT_LIBRARIES})
add_test(NAME function_test COMMAND ./function_test function_test.xml)
add_executable(module_test main.cpp module_test.cpp)
target_link_libraries(module_test ${Mruby_LIBRARIES} ${CPPUNIT_LIBRARIES})
add_test(NAME module_test COMMAND ./module_test module_test.xml)
What can I do to have cmake link correctly against cppunit?

Related

Cmake error: Target "restinio::restinio" contains relative path in its INTERFACE_INCLUDE_DIRECTORIES:

Preface: I am using Clion with Cygwin. I have installed vcpkg following their instructions. Then I followed restinio instructions to install restinio using vcpkg. Since restinio required fmt and http-parser I installed both of those too.
I have installed both the x86-windows version and x64-windows version of all 3 packages.
I linked vcpkg cmake file as per their instructions in Clion and have regenerated the CMakeCache
Currently I am trying to build specifically the x64-windows version (I was having other errors with the x86 version and I got further with the x64 version).
I have looked at this, and my initial error is different along with there is stuff inside of the directory that is in the relative path.
cmake_minimum_required(VERSION 3.23)
project(testing2)
set(CMAKE_CXX_STANDARD 14)
# RESTinio dependencies:
# 1. ASIO or Boost::ASIO (goes as headers, vcpkg knows where)
# 2. HTTP parser
find_package(unofficial-http-parser CONFIG REQUIRED)
# 3. fmtlib
find_package(fmt CONFIG REQUIRED)
# RESTinio itself
find_package(restinio CONFIG REQUIRED)
# Make your project dependent on restinio,
# and let cmake deal with all the headers paths and linked libs.
add_executable(testing2 main.cpp)
target_link_libraries(testing2 PRIVATE restinio::restinio)
cmake Options: -DCMAKE_TOOLCHAIN_FILE=C:\dev\vcpkg\vcpkg-master\scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET:STRING=x64-windows
Clion output in the cmake tab:
-- Configuring done
CMake Error in CMakeLists.txt:
Target "restinio::restinio" contains relative path in its
INTERFACE_INCLUDE_DIRECTORIES:
"C:/dev/vcpkg/vcpkg-master/installed/x64-windows/include"
CMake Error in CMakeLists.txt:
IMPORTED_LOCATION not set for imported target
"unofficial::http_parser::http_parser" configuration "Debug".
-- Generating done
CMake Error:
Running
'/cygdrive/c/Program Files/JetBrains/CLion 2021.2.3/bin/ninja/cygwin/ninja.exe' '-C' '/cygdrive/c/Users/Tally/Desktop/DevStuffs/Testing2/cmake-build-debug' '-t' 'recompact'
failed with:
ninja: error: build.ninja:35: loading 'CMakeFiles/rules.ninja': No such file or directory
include CMakeFiles/rules.ninja
^ near here
CMake Generate step failed. Build files cannot be regenerated correctly.
[Finished]
the path: "C:/dev/vcpkg/vcpkg-master/installed/x64-windows/include" exists and has asio, fmt, and restinio dirs along with asio.hpp and http_parser.h.
I have tried adding:
target_link_libraries(main PRIVATE unofficial::http_parser::http_parser) as vcpkg suggests when I install, but it gives me the same error just replaced restinio::restinio with unofficial::http_parser::http_parser.

Problem building C++ binary using vcpkg and cmake in Github action macos-12

Works fine building on Linux and Windows.
But in macos I get:
/usr/local/bin/g++-11 -isysroot /Applications/Xcode_13.4.1.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/Calculator_Test.dir/src/speedtest.cpp.o CMakeFiles/Calculator_Test.dir/src/tests.cpp.o -o Calculator_Test ../libCalculator.a ../vcpkg_installed/x64-osx/debug/lib/libCatch2d.a ../vcpkg_installed/x64-osx/debug/lib/manual-link/libCatch2Maind.a ../vcpkg_installed/x64-osx/debug/lib/libCatch2d.a
Undefined symbols for architecture x86_64:
"__ZN5Catch24translateActiveExceptionB5cxx11Ev", referenced from:
__ZN5Catch9Benchmark9Benchmark3runINSt6chrono3_V212steady_clockEEEvv in speedtest.cpp.o
"__ZN5Catch9Benchmark6Detail15analyse_samplesEdjN9__gnu_cxx17__normal_iteratorIPdSt6vectorIdSaIdEEEES8_", referenced from:
__ZN5Catch9Benchmark6Detail7analyseINSt6chrono8durationIdSt5ratioILl1ELl1000000000EEEEN9__gnu_cxx17__normal_iteratorIPS7_St6vectorIS7_SaIS7_EEEEEENS0_14SampleAnalysisIT_EERKNS_7IConfigENS0_11EnvironmentISG_EET0_SN_ in speedtest.cpp.o
Running the following works fine and without error:
cmake --preset=${{ matrix.preset }} -B build -DCalculator_Test=1 -DCMAKE_C_COMPILER=gcc-11 -DCMAKE_CXX_COMPILER=g++-11 -DCMAKE_VERBOSE_MAKEFILE=1
"preset" is just setting the CMAKE_TOOLCHAIN_FILE to the one provided by vcpkg and the cmake generator to "Unix Makefiles"
From CMakeLists.txt used:
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
...
find_package(Catch2 CONFIG REQUIRED)
target_link_libraries(Calculator_Test
PRIVATE
Calculator
Catch2::Catch2 Catch2::Catch2WithMain
)
But when compiling is when it fails
cmake --build build
My question is why does it fail and how to fix it?
Is it C++20 that is the problem or is it CMake, vcpkg or something I must do on macos. I'm no macos expert :(
Solved the problem by setting the environmental variables to the correct compiler before setting up vcpkg.
In the github actions yml file:
- name: Set C++/C compiler on macOs
shell: bash
run: echo "CC=gcc-11" >> $GITHUB_ENV; echo "CXX=g++-11" >> $GITHUB_ENV; cat "$GITHUB_ENV"
if: runner.os == 'macOs'
The build step (without specifying the c/c++ compiler):
cmake --preset=${{ matrix.preset }} -B build -DCalculator_Test=1 -DCMAKE_VERBOSE_MAKEFILE=1
My assumptions is that the linking of the dependency binaries failed earlier due to being compiled with a different compiler than the application itself.

How to use conan to handle ImGui-SFML dependency in a cmake gcc project in Linux?

I want to handle the dependencies of a C++/cmake/gcc project in Linux using conan, and build an ImGui C++ demo as shown here: using imgui by Elias Daler
I have used conan to handle Boost dependencies successfully, but with ImGui-SFML I am having a linking error.
My conanfile.txt has the following instructions:
[requires]
imgui-sfml/2.1#bincrafters/stable
[imports]
bin, *.so -> ./bin
lib, *.a -> ./lib
[generators]
cmake_find_package
cmake_paths
cmake
And I added these lines to my CMakeLists.txt to work with conan:
include(${CMAKE_BINARY_DIR}/conan_paths.cmake)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
link_directories(${CONAN_LIB_DIRS})
target_link_libraries(my_project ${CONAN_LIBS})
Then, inside build/ I run the following command to build the library and install the dependencies:
conan install .. --build imgui-sfml
So far so good with conan, the libImGui-SFML.a is generated (it is also copied to build/lib because of the [imports], though I think the copy shouldn't be required since I'm adding the link_directories() instruction).
Then, I generate the makefiles
cmake ..
Finally, when I try to build the project
cmake --build ./
I get these linking errors:
/usr/bin/ld: cannot find -lImGui-SFML
/usr/bin/ld: cannot find -lopenal
/usr/bin/ld: cannot find -lFLAC++
/usr/bin/ld: cannot find -lFLAC
The libs generated by conan are static:
libFLAC.a
libFLAC++.a
libfreetype.a
libImGui-SFML.a
libogg.a
libopenal.a
This post looks related, but didn't work for ImGui: Installing gtest with conan
Is the program looking for shared libraries?
Am I missing some configuration in the conanfile.txt or in the CMakeLists.txt file?
Edit:
Conan version 1.25.2
According to this reported issue, this solution for a similar question, and conan's basic setup documentation, in this case the CMakeLists.txt should include: adding the argument TARGET in the setup, and the call to conan_target_link_libraries instead of the usual target_link_libraries, as follows:
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)
conan_target_link_libraries(${PROJECT_NAME})
So the conanfile.txt just needs these instructions:
[requires]
imgui-sfml/2.1#bincrafters/stable
[generators]
cmake

C++ Cmake doesn't set compiler language standard on Travis CI server

I am developing an own open source programming language interpreter project and I can make silly mistakes as I am not sophisticated in Cmake and I am trying Travis CI for the first time. My project compiles on my PC (running Ubuntu 16.04LTS, GCC 8.1 and Cmake 3.12.0-rc2) from bash terminal by executing scripts/build_all.sh and tests are executed by scripts/run_tests.sh.
Build script creates a cmake-build-debug directory, runs cmake and than builds and executes simple cmake-build-debug/lexertl_gen/lexert_gen.out program to generate a header file in my include folder for the main project. After that it builds the main project alongside with it's tests all under compiler option std=gnu++2a.
Here are all my CMakeLists.txt (the problem is described bellow):
Root CMakeLists.txt
cmake_minimum_required(VERSION 3.9.0)
set(INCLUDE_ROOT "/usr/local/include")
set(BOOST_ROOT "${INCLUDE_ROOT}/boost_1_67_0")
set(LEXERTL_ROOT "${INCLUDE_ROOT}/lexertl")
set(GTEST_ROOT "${INCLUDE_ROOT}/googletest")
set(CMAKE_CXX_FLAGS "-g -std=gnu++2a -Werror \
-Wno-error=variadic-macros \
-Werror=implicit-fallthrough")
file(GLOB SOURCES "src/*.cpp")
include_directories(include ${BOOST_ROOT} ${LEXERTL_ROOT} ${GTEST_ROOT})
add_library(calamity_core ${SOURCES})
project(lexer.out)
add_executable(${PROJECT_NAME} main_lexer.cpp)
target_link_libraries(${PROJECT_NAME} m calamity_core)
project(parser.out)
add_executable(${PROJECT_NAME} main_parser.cpp)
target_link_libraries(${PROJECT_NAME} m calamity_core)
project(tests.out)
add_subdirectory(tests)
project(lexertl_gen.out)
add_subdirectory(lexertl_gen)
tests/CMakeLists.txt
cmake_minimum_required(VERSION 3.9.0)
set(CMAKE_CXX_FLAGS "-g -std=gnu++2a -Werror \
-Wno-error=variadic-macros \
-Werror=implicit-fallthrough")
add_subdirectory(/usr/local/include/googletest ../gtets-build)
add_executable(${PROJECT_NAME} tests.cpp)
target_link_libraries(${PROJECT_NAME} gtest gtest_main gmock calamity_core)
lexertl_gen/CMakeLists.txt
cmake_minimum_required(VERSION 3.9.0)
set(CMAKE_CXX_FLAGS "-g -std=gnu++2a -Werror \
-Wno-error=variadic-macros \
-Werror=implicit-fallthrough")
set(SOURCES ../src/token.cpp
../src/var_substring.cpp
../src/cui.cpp
../src/message_exception.cpp)
add_executable(lexertl_gen.out main_lexertl_gen.cpp ${SOURCES})
Ultimately, I decided to add Travis CI to my project. I wrote a bootstrap.sh script to install and configure all dependencies (I also install GCC 8.1, because default GCC 4.8.4 installed on Travis CI server doesn't support gnu++2a). This script works well, but when it comes to running scripts/build_all.sh on Travis CI server GCC fails to set gnu++2a for compiling my main project with tests, it complains about using inline variables, std::is_integral and other modern C++ stuff, explicitly mentioning that those features are supported under std=c++17 or std=gnu++17. Here is the link to my latest unsuccessful build on Travis CI.
I don't get it, as I updated GCC and my first simple code generator program runs well (though if I remove set(CMAKE_CXX_FLAGS ...) from tests/CMakeLists.txt this program compiles on my PC but doesn't compile on Travis CI server either). What's wrong am I doing here? Is it because of the Cmake 3.9 which Travis uses?
My .travis.yml configuration file:
language: cpp
sudo: required
dist: trusty
compiler:
- g++
os:
- linux
script:
- ./bootstrap.sh; cd scripts; ./build_all.sh; ./run_tests.sh
env:
global:
- LANG="en_US.UTF-8"
matrix:
include:
- os: linux
addons:
apt:
compiler: gcc
sources:
- ubuntu-toolchain-r-test
packages:
- g++-8
env: COMPILER=g++-8
Link to my project on GitHub. (sorry for empty README.md)
Well I've finally solved a problem by making CMAKE_CXX_FLAGS a cache variable:
set(CMAKE_CXX_FLAGS "-g -std=gnu++2a -Werror \
-Wno-error=variadic-macros \
-Werror=implicit-fallthrough"
CACHE STRING "compiler flags")
Though I don't think it is a beautiful solution and I still can't understand why it compiled on my PC but not on Travis CI

Run npm command before specific cmake target

I am trying to build QT based application with CMake and everything goes well enough. I followed this tutorial and I can build my application. Now I want to run npm run build before build using add_custom_command it does not seems to goes as expected.
The build process fails with
RCC: Error in 'tray-icon/systray.qrc': Cannot find file 'html/js/app.full.js'
AUTORCC: error: process for.build/Debug/x64/tray-icon/CMakeFiles/tray-icon.dir/qrc_systray.cpp failed:
RCC: Error in 'tray-icon/systray.qrc': Cannot find file 'html/js/app.full.js'
Can you tell me how to execute npm command before the build verification step on tray-icon/systray.qrc?
This is my cmake file
cmake_minimum_required(VERSION 2.8.11)
project(tray-icon)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package(Qt5Widgets 5.5)
find_package(Qt5Qml 5.5)
find_package(Qt5WebEngine 5.5)
find_package(Qt5WebEngineCore 5.5)
find_package(Qt5WebEngineWidgets 5.5)
# generate rules for building source files from the resources
set(SOURCES tray-icon.cpp window.cpp systray.qrc)
set(CMAKE_VERBOSE_MAKEFILE 1)
#adds target
add_executable(tray-icon ${SOURCES})
# custom build command for javascript part of the application
add_custom_command (
TARGET "tray-icon"
PRE_BUILD COMMAND npm run build
)
# Find the QtWidgets library
target_link_libraries(tray-icon
Qt5::Widgets
Qt5::WebEngine
Qt5::WebEngineWidgets)
install(TARGETS tray-icon DESTINATION .)
PS: My final solution looks like this
# custom build command for javascript part of the application
add_custom_target(
tray-icon_automoc
)
add_custom_target (
npm-target
COMMAND cd ${PROJECT_SOURCE_DIR} && cd html && npm install && npm run build
)
You could try add_custom_target and add_dependencies.
add_custom_target (
npm-target
COMMAND npm run build
)
add_dependencies(tray-icon npm-target)
To overcome the issue mentioned in the comments you should be able to add dependency using AUTOGEN_TARGET_DEPENDS target property. It can be set instead to a list of dependencies for the _automoc target.