Make C Project Instead of C++ - c++

I am having trouble setting up a C projectin Clion. I changed the name of the main.cpp to main.c, and altered the CMakeLists file accordingly, with the following info:
cmake_minimum_required(VERSION 3.3)
project(Project_1__)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.c)
add_executable(Project_1__ ${SOURCE_FILES})
However I get the following error when I try to build:
fatal error: iostream: No such file or directory #include compilation terminated. mingw32-make.exe[3]: * [CMakeFiles/Project_1__.dir/main.c.obj] Error 1 mingw32-make.exe[2]:
* [CMakeFiles/Project_1__.dir/all] Error 2

This error is due to fact that your complier is unable to find iostream header file. Make sure it exists in that directory. Plus it also depends on your IDE.
Also make sure, it is declared as #include "iostream.h" instead of #include<iostream.h>
iostream header files are declared in many of the compilers is declared by #include<iostream>
C++ needs <iostream> header file while C needs only <stdio.h>

Replace #include <iostream> with #include <stdio.h>. You'll also have to replace the stream related stuff in your code with stdio functions. See man stdio.
Also, you may have to change your makefile as I see -std=c++11 and I suspect that should be -std=c11. I'm not completely clear on this, so YMMV

iostream.h is specific to c++, c do not use stream classes

Related

Problem with CMake finding my header file

So I am trying to compile a small project that I am doing of a Sokoban Solver. For that, I have the following folder structure:
The CMakeLists file that I have generated is quite simple and contains the following lines of code:
project(Sokoban)
cmake_minimum_required(VERSION 3.16.3)
include_directories(include/)
add_executable(main src/main.cpp src/coord.cpp src/map.cpp src/node_data.cpp src/node.cpp src/search_methods.cpp)
As I understand it:
Executable needs to have the *.cpp files
The directory with the *.hpp files must be included somehow to the CMake so the compiler knows where to look for them.
When I try to compile, I obtain the following error:
[build] ../src/main.cpp:4:10: fatal error: include/search_methods.hpp: No such file or directory
[build] 4 | #include "include/search_methods.hpp"
[build] | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
[build] compilation terminated.
Does anyone know how or which other information would you need to solve this issue? I have been looking into similar problems, but did not solve the issue.
As #tkausl pointed out
Either drop the include folder from your #include statements or from your include path; can't have both.
Change #include "include/search_methods.hpp" to #include "search_methods.hpp" and it should work.
Also consider to use a more modern CMake approach using
target_include_directories(main
PUBLIC
include
)
and git rid of include_directories(include/)

cmake: add directory to be searched for header files

I am writing c++ code to run tests on an arduino project I've written. In order to run it, I need it to include mockups of a couple libraries (the original libraries don't work in c++). The mockups are in my src directory, along with the tests in main.cpp. The files that rely on the mockups are in the parent directory of src, and I cannot modify them to refer to src without breaking their ability to run when uploaded to an arduino. I also cannot add the mockups or main.cpp to the parent directory, as this would interfere with the operation of the arduino code.
So, I need to add the child directory with the mockups to the directories that are searched when compiling the files in the parent directory.
Directory Outline:
parent
forTest.h
forTest.cpp
src
parson.h
parson.c
String.h
String.cpp
main.cpp
CMakeLists.txt
build
In this case, main.cpp has "#include ../forTest.cpp", and forTest.cpp has "#include parson.h" and "#include String.h"
I am currently running the following code in CMakeLists.txt:
cmake_minimum_required (VERSION 2.6)
project(makeTest)
include_directories(../ )
set (makeTest_VERSION_MAJOR 1)
set (makeTest_VERSION_MINOR 0)
add_executable(makeTest main.cpp ../forTest.cpp String.cpp parson.c)
I then build it in the build directory from the command line with
cmake -G "Unix Makefiles" ../src
make
The cmake command resolves successfully, but the make command hits "fatal error: parson.h not found" when building forTest.cpp
How can I resolve this?
edit: my apologies if this has an obvious answer, I'm very new to Cmake
edit the second: using the changes suggested by Gergely Nyiri and changing #include to #include "/usr/include/string.h" resolves several errors, but introduces a new error : "implicit instantiation of undefined template" during the make step.
It refers to the following code in by string.h mockup file:
#ifndef STRING_H
#define STRING_H
#include "/usr/include/string.h"
using namespace std;
class String {
private:
std::string thisString;
char chars[];
...
#endif
which returns the error:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cstring:61:
/Users/douglas/Desktop/parts/makeHolder/testMake/src/string.h:9:14: error:
implicit instantiation of undefined template 'std::__1::basic_string<char,
std::__1::char_traits<char>, std::__1::allocator<char> >'
std::string thisString;
This is followed by:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/iosfwd:193:32: note:
template is declared here
class _LIBCPP_TEMPLATE_VIS basic_string;
First of all, I would propose to put CMakeLists.txt in your source root. Then:
cmake_minimum_required (VERSION 2.6)
project(makeTest)
include_directories(src)
set (makeTest_VERSION_MAJOR 1)
set (makeTest_VERSION_MINOR 0)
add_executable(makeTest src/main.cpp forTest.cpp src/String.cpp src/parson.c)
Configure & build:
cd build
cmake ../
make
Even better if you use target_include_directories instead of include_directories:
..
add_executable(makeTest src/main.cpp forTest.cpp src/String.cpp src/parson.c)
target_include_directories(makeTest PRIVATE src)

include_directories do not include directories

Referring the original project as it is easier to explain.
I'm trying to compile this project:
https://github.com/jbohg/render_kinect
I get the error
/home/sarkar/libs/render_kinect/src/kinectSimulator.cpp:57:43: fatal
error: render_kinect/kinectSimulator.h: No such file or directory
#include <render_kinect/kinectSimulator.h>
^
When clearly kinectSimulator.h is present in the include directory. And
include_directories(${PROJECT_SOURCE_DIR}/include)
present in the cmake.
I changed #include <render_kinect/kinectSimulator.h> to #include "render_kinect/kinectSimulator.h".
Added include_directories(${PROJECT_SOURCE_DIR}/include/renderer_kinect) to cmake followed by #include "kinectSimulator.h" in the source
Nothing seems to work. Looks like include_directories function is not affecting at all. Though the paths show up in the
get_property(inc_dirs DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
message("inc_dirs = ${inc_dirs}")
gives
inc_dirs = /home/sarkar/libs/render_kinect/include;other/include/paths
Why is this still not working?
EDIT:
make verbose do not show -I option.
make VERBOSE=1
/usr/bin/c++ <options> -o CMakeFiles/RenderKinect.dir/src/kinectSimulator.o -c
/home/sarkar/libs/render_kinect/src/kinectSimulator.cpp
/home/sarkar/libs/render_kinect/src/kinectSimulator.cpp:57:43: fatal
error: render_kinect/kinectSimulator.h: No such file or directory
Are the INCLUDE_DIRECTORIES properties completely being ignored?

g++ #include file not found compiler error

I'm currently trying to simply link a file called main.cpp with boost in order to do asio(asynchronous input and output). I'm using a makefile in order to compile the C++ code into executable form. However, despite using the -I modifier on my terminal command in Mac OS X, it still cannot find the appropriate directory to search in. Here is my code in Main
#include <asio.hpp>
int main(){
return 1;
}
and here is the makefile command that I am using
all:
g++ -Iboost_1_60_0/boost/ -o main main.cpp
In my file structure, boost_1_60_0, main.cpp, and makefile are all on the top level, where asio is in the folder boost which is in the folder boost_1_60_0. I'm very confused about this and any help would be appreciated! Thanks!
EDIT: Full error is
1 error generated.
make: *** [all] Error 1
iMats-2:SerialC++ wfehrnstrom$ make
g++ -Iboost_1_60_0 -o main main.cpp
main.cpp:2:10: fatal error: 'asio.hpp' file not found
You should use -Iboost_1_60_0 (or, better still, install Boost properly so that it's found automatically under /usr/include/), and write #include <boost/asio.hpp>.
The include directives inside Boost itself will assume this form, so…
BTW, this has nothing to do with exceptions. Presumably it's a compiler error you've seen.

"fatal error: 'chrono' file not found" when compiling C++ project based on CMake

Do you know what that error message means and what I should do now?
vpn-global-dhcp3-252:build Dolyn$ make
Scanning dependencies of target mdatom
[ 10%] Building CXX object CMakeFiles/mdatom.dir/src/main.cpp.o
/Users/Dolyn/sources/src/main.cpp:124:11: fatal error:
'chrono' file not found
#include <chrono>
For high resolutio timings
^
1 error generated.
I have no idea about what I have to do now.
The build error probably means that the source file includes a C++11 header (<chrono>) but you are compiling without support for C++11.
Given that your compiler supports C++11, you need to activate it using the following line in the CMakeLists.txt file:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")