I am trying to generate a precompiled header in cmake that contains my regularly used standard libs. When I run cmake there is no errors but when I build it says it can't find the headers in cmake_pch.h.
Here is the snippet of my cmake script that adds the precompiled header:
target_precompile_headers(fae-core PRIVATE
<algorithm>
<cstddef>
<fstream>
<string>
<sstream>
<memory>
<chrono>
)
Here is the full output from running the makefile that cmake generates:
Scanning dependencies of target fae-core
[ 6%] Building CXX object core/CMakeFiles/fae-core.dir/cmake_pch.hxx.gch
[ 12%] Building C object core/CMakeFiles/fae-core.dir/cmake_pch.h.gch
In file included from <command-line>:32:
/home/finn/dev/fae/build/core/CMakeFiles/fae-core.dir/cmake_pch.h:4:10: fatal error: algorithm: No such file or directory
4 | #include <algorithm>
| ^~~~~~~~~~~
compilation terminated.
make[2]: *** [core/CMakeFiles/fae-core.dir/build.make:78: core/CMakeFiles/fae-core.dir/cmake_pch.h.gch] Error 1
make[1]: *** [CMakeFiles/Makefile2:136: core/CMakeFiles/fae-core.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
I've only included the section of my cmake code relating to the pch as I was previously just including the libs directly in my src files and everything was working fine so I assume it's purely related to the pch. I'm happy to edit and add the rest of my scripts if it's helpful.
You have added C++ headers to your target_precompile_headers command but also added a C source to your target. This cannot work as the C compiler does not understand how to include C++ standard headers. See the error message:
[ 12%] Building C object core/CMakeFiles/fae-core.dir/cmake_pch.h.gch
Either remove the C++ headers from your target or define separate targets for C and C++ sources.
Related
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.
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..
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
So I am struggeling with cmake for some time now. I want to use the xmlrpc-c library from here. So I started a new project with main.cpp and CMakeLists.txt and copied the xmlrpc-c as a subdirectory into my project (since xmlrpc-c is unfortunately not a cmake library):
My code is exactly a example from here and looks like this:
#include <iostream>
#include <string>
#include "xmlrpc-c/include/xmlrpc-c/base.hpp"
#include "xmlrpc-c/include/xmlrpc-c/registry.hpp"
#include "xmlrpc-c/include/xmlrpc-c/server_abyss.hpp"
using namespace std;
class hello : public xmlrpc_c::method
{
public:
void execute(const xmlrpc_c::paramList& params, xmlrpc_c::value* retval)
{
string msg(params.getString(0));
params.verifyEnd(1);
cout << msg << endl;
*retval = xmlrpc_c::value_string("XMLRPC server says hello!");
}
};
int main(int argc, char** argv)
{
xmlrpc_c::registry registry;
registry.addMethod("hello", new hello);
xmlrpc_c::serverAbyss server(xmlrpc_c::serverAbyss::constrOpt().registryP(®istry).portNumber(8080));
server.run();
return 1;
}
CMakeLists.txt looks like this
cmake_minimum_required(VERSION 3.16)
project(xmlrpc_c_server C CXX)
add_executable(xmlrpc_c_server main.cpp)
target_link_libraries(xmlrpc_c_server -lxmlrpc++ -lxmlrpc_server++ -lxmlrpc_server_abyss++ -lxmlrpc_util++)
The problem I have is that my build-process fails with a linker-error: as far as I understand is the header file registry.hpp not included correctly. If I comment out the code line registry.addMethod("hello", new hello);, I can compile the program without any errors.
====================[ Build | xmlrpc_c_server | Debug ]=========================
/usr/bin/cmake --build /mnt/c/Users/valentin.ackva/CLionProjects/xmlrp-c-server/cmake-build-debug --target xmlrpc_c_server -- -j 9
Scanning dependencies of target xmlrpc_c_server
[ 50%] Building CXX object CMakeFiles/xmlrpc_c_server.dir/main.cpp.o
[100%] Linking CXX executable xmlrpc_c_server
/usr/bin/ld: CMakeFiles/xmlrpc_c_server.dir/main.cpp.o: in function `main':
/mnt/c/Users/struppel/CLionProjects/xmlrp-c-server/main.cpp:31: undefined reference to `xmlrpc_c::registry::addMethod(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, xmlrpc_c::method*)'
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/xmlrpc_c_server.dir/build.make:84: xmlrpc_c_server] Error 1
make[2]: *** [CMakeFiles/Makefile2:76: CMakeFiles/xmlrpc_c_server.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/xmlrpc_c_server.dir/rule] Error 2
make: *** [Makefile:118: xmlrpc_c_server] Error 2
What is missing?
Since the library you want to use doesn't have a cmake project you need to handle this on your own. I would suggest using add_subdirectory and creating a sub-project, where you build the library (either as shared or static depending on your needs).
In addition you need to point cmake to the location of the headers. Try manually adding what's missing to the add_executable or use include_directories and adjust your #includes accordingly.
You can also use some unofficial cmake version of it like this one. If you are using git you can add the repo as a submodule and integrate the code from that repo into your main project.
Since you are new to CMake maybe it's worth looking at a tutorial instead?
https://cliutils.gitlab.io/modern-cmake/
I think it would help your confusion a lot.
Ive downloaded the SDL2 library using brew install SDL2 but when I am trying to compile melonDS for Mac, I keep banging my head against the wall of the annoying error of
bash-3.2$ make
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/bigman/melonDS/build
[ 1%] Building CXX object CMakeFiles/melonDS.dir/src/libui_sdl/main.cpp.o
/Users/bigman/melonDS/src/libui_sdl/main.cpp:24:10: fatal error:
'SDL2/SDL.h' file not found
#include <SDL2/SDL.h>
^~~~~~~~~~~~
1 error generated.
make[2]: *** [CMakeFiles/melonDS.dir/src/libui_sdl/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/melonDS.dir/all] Error 2
make: *** [all] Error 2
bash-3.2$
I've uninstalled, reinstalled, tried to use compiler flags in the makefile (which did't work because the project uses cmake) but to no avail. Any other reccomendations would be great.