How can I run many C++ source files in one CLion project? - c++

I am using CLion as an IDE. When I create a project a main.cpp is automatically added. I would like to add 30-40 cpp files in a project and keep them as one project. Basically, I just wanna create many .cpp files in one folder and make CLion run them. I can do this in Pycharm by simply creating a project and add as many .py files as I want. But when I want to do this on CLion I got an error. Is it possible to add many .cpp files in a project in CLion, if yes, how can I do that?
An error could be seen in the below. I added a second.cpp to the project and run and this error message appears.
====================[ Build | trial | Debug ]===================================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/mertsaner/CLionProjects/trial/cmake-build-debug --target trial -- -j 6
Scanning dependencies of target trial
[ 66%] Building CXX object CMakeFiles/trial.dir/main.cpp.o
[ 66%] Building CXX object CMakeFiles/trial.dir/second.cpp.o
[100%] Linking CXX executable trial
duplicate symbol '_main' in:
CMakeFiles/trial.dir/main.cpp.o
CMakeFiles/trial.dir/second.cpp.o
ld: 1 duplicate symbol for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [trial] Error 1
make[2]: *** [CMakeFiles/trial.dir/all] Error 2
make[1]: *** [CMakeFiles/trial.dir/rule] Error 2
make: *** [trial] Error 2

Clion uses Cmake. If you want to create multiple executable files for eg with names (ex1.cpp, ex2.cpp. ex3.cpp) in one directory, you will do something like this in the CMake file of your directory.
cmake_minimum_required(VERSION 3.18)
project(some_project)
set(CMAKE_CXX_STANDARD 20)
add_executable(executable1 ex1.cpp)
add_executable(executable2 ex2.cpp)
add_executable(executable3 ex3.cpp)
and so on..

Related

How to add cryptopp to project in OMNeT++

First, I built the cryptlib (of cryptopp) in Visual Studio Code 2022. And tried a sample c++ program (using files from the library) and it worked fine.
Then, I included the library cryptlib.lib in my OMNeT++ project.
I also included the cryptopp folder for the .cpp and .h files of the library.
But when I build the omnet++ project, I get this error:
03:05:22 **** Incremental Build of configuration debug for project crypto_final ****
make MODE=debug all
cd src && /usr/bin/make
make1: Entering directory '/d/omnetpp-5.7/samples/crypto_final/src'
Server.cc
Creating executable: ../out/clang-debug/src/crypto_final_dbg.exe
lld-link: error: could not open 'liblibcpmt.a': No such file or directory
lld-link: error: could not open 'libLIBCMT.a': No such file or directory
lld-link: error: could not open 'libOLDNAMES.a': No such file or directory
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
make[1]: *** [Makefile:99: ../out/clang-debug/src/crypto_final_dbg.exe] Error 1
make[1]: Leaving directory '/d/omnetpp-5.7/samples/crypto_final/src'
make: *** [Makefile:2: all] Error 2
"make MODE=debug all" terminated with exit code 2. Build might be incomplete.
03:05:25 Build Failed. 2 errors, 0 warnings. (took 3s.352ms)
I don't even know what are these libraries or how to get them built.
Can anyone help, please?
Edit #1: btw, when I change the Target type from "Executable" to "Static library (.lib or .a) in makemake options, the project builds normally but does not run properly (has some bugs and no effects can appear on the simulation)
OMNeT++ project uses Makefile, therefore to add an external library or class one should modify makefrag.
You should go to Project | Properties | OMNeT++ | Makemake | select root or src of your project | Options, then Custom | Makefrag and write the following lines:
EXTRA_OBJS += -L/d/foo/lib -llibcpmt -lLIBCMT -lOLDNAMES
CFLAGS += -I/d/foo/include
where /d/foo/lib is an example of the directory which contains your cryptlib static files (e.g. liblibcpmt.a, libLIBCMT.a, ... ), and /d/foo/include - the directory that contains header files of cryptlib.
Omnet++ useses MinGW as the runtime system and the C++ compiler's ABI is incompatible with the MS ABI (i.e. C++ code generated by Visual Studio C++ compiler is incompatible with the code generted by gcc or clang).
You MUST compile your crypto library also with the clang compiler coming with OMNeT++. Visual Studio compiler will NOT work.
Or... OpenSSL libraries and headers are already available in the Windows distro, so you can pt to use that.

Make can't find .hpp file in Boost

I'm using an Ubuntu virtual machine and am encountering the following error message when running the 'make' command:
Scanning dependencies of target AIToolboxMDP
[ 1%] Building CXX object src/CMakeFiles/AIToolboxMDP.dir/Impl/Seeder.cpp.o
[ 1%] Building CXX object src/CMakeFiles/AIToolboxMDP.dir/Impl/CassandraParser.cpp.o
In file included from /home/ben/AI/AI-Toolbox-master/include/AIToolbox/Impl/CassandraParser.hpp:4,
from /home/ben/AI/AI-Toolbox-master/src/Impl/CassandraParser.cpp:1:
/home/ben/AI/AI-Toolbox-master/include/AIToolbox/Types.hpp:7:10: fatal error: boost/multi_array.hpp: No such file or directory
7 | #include <boost/multi_array.hpp>
| ^~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make[2]: *** [src/CMakeFiles/AIToolboxMDP.dir/build.make:76: src/CMakeFiles/AIToolboxMDP.dir/Impl/CassandraParser.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:1140: src/CMakeFiles/AIToolboxMDP.dir/all] Error 2
make: *** [Makefile:95: all] Error 2
I ran cmake right before make, and cmake was able to find Boost; I can also see that the multi_array.hpp file is in the folder /home/ben/AI/boost_1_77_0/boost, so I'm not sure why make can't find the file. I tried adding variations of the line target_link_libraries(program ${Boost_LIBRARIES}) into the CMakeLists.txt file and using variations of the -L/-l options with the make call, and neither method worked (although I wasn't sure which program name to use with target_link_libraries so I tried a bunch of guesses but maybe I didn't use the right one; I'm trying to build AI-Toolbox if that helps).
I also have to add several options to the cmake command I run right before make in order to get rid of any errors with that; here's what I enter in case that would do anything or if I need to add something else in there:
cmake .. -DBOOST_ROOT=/home/ben/AI/boost_1_77_0 -DLPSOLVE_INCLUDE_PATH=/home/ben/AI/lpsolve -DVCPKG_TARGET_TRIPLET=x64-linux -DCMAKE_TOOLCHAIN_FILE=/home/ben/AI/vcpkg/scripts/buildsystems/vcpkg.cmake
My CMakeLists.txt file contains the following lines (among others, but these seem like the most relevant) just in case that helps as well:
find_package(Boost ${BOOST_VERSION_REQUIRED} REQUIRED)
include_directories(SYSTEM ${Boost_INCLUDE_DIRS})
Would anyone have any ideas as to how I can get past the make error?
I ended up solving it by deleting my Boost folder and reinstalling it using the command sudo apt-get install libboost-all-dev; I just reran cmake and took out all the options I had been using except for the lpsolve one, and then when I ran make afterward I encountered no issues.

Building OpenCV using makefile throwing error?

I was trying to setup OpenCV environment using MingW64 and visual studio code,
Work done so far
Installed and set environment CMake
Installed OpenCV 4.5.2
OpenCV Makefile generation
using CMake Build from source(openCV directory)
specified the generator for the project
configured the compiler type and makefile type
Enabled precompiled header
Confusing part
when I entered the command mingw32-make for compilation in directory where the makefile is generated
It worked until 67% after that:
[ 67%] Built target pch_Generate_opencv_test_dnn
Scanning dependencies of target opencv_test_dnn
[ 67%] Building CXX object modules/dnn/CMakeFiles/opencv_test_dnn.dir/test/npy_blob.cpp.obj
In file included from <command-line>:0:0:
D:/opencv/build/modules/dnn/test_precomp.hpp:50:10: fatal error: test_common.hpp: No such file or directory
#include "test_common.hpp"
^~~~~~~~~~~~~~~~~
compilation terminated.
mingw32-make[2]: *** [modules\dnn\CMakeFiles\opencv_test_dnn.dir\build.make:82: modules/dnn/CMakeFiles/opencv_test_dnn.dir/test/npy_blob.cpp.obj] Error 1
mingw32-make[1]: *** [CMakeFiles\Makefile2:3429: modules/dnn/CMakeFiles/opencv_test_dnn.dir/all] Error 2
mingw32-make: *** [Makefile:181: all] Error 2
thank you

Clion: How to configure clion when Opening existing folder there is no debug and run menu?

I'm using CLion 2021.1.1 version. I'm a new user. I've done some cpp programs on codeblocks. But Whenever I open an existing folder the files are showing but there is no debug option. It says Add configuration. There is no run option. I've added MingW in buil, execution, Deployment -> Toolchains-> mingw but still it says add configuration. I've close it open new projects in CPP but still getting add configuration. How can open the existing folder and it has many cpp files that I can run each and every cpp file. How can achieve this?
sometimes I get this error:
CMake Error at C:/Program Files/JetBrains/CLion 2021.1.1/bin/cmake/win/share/cmake-3.19/Modules/CMakeTestCCompiler.cmake:66 (message):
The C compiler
"C:/MinGW/bin/gcc.exe"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: O:/1.Fourth Semester/Programming/Algorithm/cmake-build-debug/CMakeFiles/CMakeTmp
Run Build Command(s):C:/MinGW/bin/mingw32-make.exe cmTC_d8b71/fast && C:/MinGW/bin/mingw32-make.exe -f CMakeFiles\cmTC_d8b71.dir\build.make CMakeFiles/cmTC_d8b71.dir/build
mingw32-make.exe[1]: Entering directory 'O:/1.Fourth Semester/Programming/Algorithm/cmake-build-debug/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_d8b71.dir/testCCompiler.c.obj
C:\MinGW\bin\gcc.exe -o CMakeFiles\cmTC_d8b71.dir\testCCompiler.c.obj -c "O:\1.Fourth Semester\Programming\Algorithm\cmake-build-debug\CMakeFiles\CMakeTmp\testCCompiler.c"
CMakeFiles\cmTC_d8b71.dir\build.make:83: recipe for target 'CMakeFiles/cmTC_d8b71.dir/testCCompiler.c.obj' failed
mingw32-make.exe[1]: *** [CMakeFiles/cmTC_d8b71.dir/testCCompiler.c.obj] Error 1
mingw32-make.exe[1]: Leaving directory 'O:/1.Fourth Semester/Programming/Algorithm/cmake-build-debug/CMakeFiles/CMakeTmp'
Makefile:139: recipe for target 'cmTC_d8b71/fast' failed
mingw32-make.exe: *** [cmTC_d8b71/fast] Error 2
CMake will not be able to correctly generate this project
Someone help, please

CMake linker cannot find library that does not begin with "lib"

I am using CMake to compile an application which uses the HSImage library on github. After installing with pip, the HSI library generates a shared library file, in my case it is created at /usr/src/HSI/HSI.cpython-36m-aarch64-linux-gnu.so
I am trying to link this library to my application with CMake, but the CMake find_library method is having some trouble finding the library. Here is the relevant part of my CMakeLists.txt file:
CMakeLists.txt
set(HSI_DIR /usr/src/HSI)
find_library(HSI_LIB HSI.cpython-36m-aarch64-linux-gnu PATHS ${HSI_DIR})
message(STATUS "HSI: ${HSI_LIB}") # outputs /usr/src/HSI/HSI.cpython-36m-aarch64-linux-gnu.so
add_executable(${TARGET_NAME} <sources...>)
target_link_directories(${TARGET_NAME} PUBLIC ${HSI_DIR})
target_link_libraries(${TARGET_NAME}
${HSI_LIB}
<other libs...>
-Wl,--unresolved-symbols=ignore-in-shared-libs
)
When building, this produces the following error message:
cd /home/nvidia/projects/HsiInference/build;/usr/local/bin/cmake --build "/home/nvidia/projects/HsiInference/build" --target hsi_inference_onnx -- ;
Scanning dependencies of target hsi_inference_onnx
[ 14%] Building CXX object CMakeFiles/hsi_inference_onnx.dir/targets/HsiInferenceOnnx/main_onnx.cpp.o
[ 28%] Building CXX object CMakeFiles/hsi_inference_onnx.dir/targets/HsiInferenceOnnx/HsiInferenceOnnx.cpp.o
[ 42%] Building CXX object CMakeFiles/hsi_inference_onnx.dir/src/ftpnano.cpp.o
[ 57%] Building CXX object CMakeFiles/hsi_inference_onnx.dir/src/getOptions.cpp.o
[ 71%] Building CXX object CMakeFiles/hsi_inference_onnx.dir/src/logger.cpp.o
[ 85%] Building CXX object CMakeFiles/hsi_inference_onnx.dir/src/utils.cpp.o
[100%] Linking CXX executable hsi_inference_onnx_debug
CMakeFiles/hsi_inference_onnx.dir/build.make:245: recipe for target 'hsi_inference_onnx_debug' failed
CMakeFiles/Makefile2:123: recipe for target 'CMakeFiles/hsi_inference_onnx.dir/all' failed
**/usr/bin/ld: cannot find -lHSI.cpython-36m-aarch64-linux-gnu**
**collect2: error: ld returned 1 exit status**
make[3]: *** [hsi_inference_onnx_debug] Error 1
make[2]: *** [CMakeFiles/hsi_inference_onnx.dir/all] Error 2
CMakeFiles/Makefile2:130: recipe for target 'CMakeFiles/hsi_inference_onnx.dir/rule' failed
make[1]: *** [CMakeFiles/hsi_inference_onnx.dir/rule] Error 2
Makefile:196: recipe for target 'hsi_inference_onnx' failed
make: *** [hsi_inference_onnx] Error 2
Build failed.
The important part:
/usr/bin/ld: cannot find -lHSI.cpython-36m-aarch64-linux-gnu
collect2: error: ld returned 1 exit status
From what I have gathered, target_link_libraries simply adds -l<library_name> to the link command, and -l<library_name> assumes that there is a file called lib<library_name>.so to link, which is not the case here. How can I get CMake to link the library properly despite the weird filename?
NOTE:
I am able to get the project to build by doing the following:
Delete the project's build directory to clear CMake caches
Rename the file or create a symbolic link to libhsi.so
Change CMakeLists.txt to find_library(HSI_LIB hsi PATHS ${HSI_DIR})
This changes the link command to -lhsi instead, which is able to find the renamed/soft-linked library file. HOWEVER, this is not ideal for me and the original question remains unanswered :)
For libraries with weird filename you should add : before the filename. Be careful, like mentioned in https://linux.die.net/man/1/ld : If namespec is of the form :filename, ld will search the library path for a file called filename, otherwise it will search the library path for a file called libnamespec.a (namespec is what comes after -l).
For your example you should replace ${HSI_LIB} in target_link_libraries by :${HSI_LIB}.so.