fatal error: string: No such file or directory - c++

I get this message upon compiling with cmake:
In file included from scanner.lex:6:
C:\Users\uriya\CLionProjects\untitled1\output.hpp:4:10: fatal error: string: No such file or directory
4 | #include <string>
| ^~~~~~~~
compilation terminated.
make.exe[3]: *** [CMakeFiles\untitled1.dir\build.make:88: CMakeFiles/untitled1.dir/lex.yy.c.obj] Error 1
make.exe[3]: *** Waiting for unfinished jobs....
make.exe[2]: *** [CMakeFiles\Makefile2:72: CMakeFiles/untitled1.dir/all] Error 2
make.exe[1]: *** [CMakeFiles\Makefile2:84: CMakeFiles/untitled1.dir/rule] Error 2
make.exe: *** [Makefile:117: untitled1] Error 2
This is the part of the code that causes the problems:
#ifndef _236360_2_
#define _236360_2_
#include <string>
using namespace std;
namespace output {
extern const string rules[];
void printProductionRule(int ruleno);
void errorLex(int lineno);
void errorSyn(int lineno);
};
#endif
and that's the cmake input:
cmake_minimum_required(VERSION 3.13)
project(untitled1)
set(CMAKE_CXX_STANDARD 11)
add_executable(untitled1 parser.tab.cpp output.cpp lex.yy.c)
oddly enough I manage to compile it with no problems using g++ -std=c++11 -o hw2 parser.tab.cpp output.cpp lex.yy.c
what's the difference between cmake and this command?

Related

catch2 throws and error when adding a struct

This is the root of my project. I think I am missing a basic concept because the error occur when I wrap the the find() function in a struct.
CMakeLists.txt
bst.cpp
bst.hpp
bst-test.cpp
catch.hpp
CMakeLists.txt
cmake_minimum_required(VERSION 3.16.4 FATAL_ERROR)
project(bst LANGUAGES CXX)
set(CMAKE_CXX_COMPILER clang++)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_library(bst bst.cpp)
add_executable(bst-test bst-test.cpp)
target_link_libraries(bst-test bst)
enable_testing()
add_test(
NAME catch_test
COMMAND $<TARGET_FILE:bst-test> --success
)
bst.cpp
struct Bst {
int find(int num, int array[]) { return -1; }
};
bst.hpp
struct Bst {
int find(int num, int array[]);
};
bst-test.cpp
#include "bst.hpp"
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
TEST_CASE("The number to search is not found in the list", "[notFound]") {
int array[]{};
Bst tree;
REQUIRE(tree.find(1, array) == -1);
}
This is the error when trying to compile.
CMakeFiles/bst-test.dir/bst-test.cpp.o: In function `____C_A_T_C_H____T_E_S_T____0()':
bst-test.cpp:(.text+0x2b0b5): undefined reference to `Bst::find(int, int*)'
clang-11: error: linker command failed with exit code 1 (use -v to see invocation)
CMakeFiles/bst-test.dir/build.make:84: recipe for target 'bst-test' failed
make[2]: *** [bst-test] Error 1
CMakeFiles/Makefile2:104: recipe for target 'CMakeFiles/bst-test.dir/all' failed
make[1]: *** [CMakeFiles/bst-test.dir/all] Error 2
Makefile:94: recipe for target 'all' failed
make: *** [all] Error 2
you are declaring the Bst structure again in your .cpp file. The source file should only contain the definition of the methods, not the declaration of the structure.
Changing bst.cpp to the following fixes the error:
#include "bst.hpp"
int Bst::find(int num, int array[]) { return -1; }

How to fix: CMake-defined macro won't be received by compiler

SOLVED! = instead of - in the CMakeLists.txt in say-hello directory
I have problems with defining a preprocessor variable with CMake according to vector-of-bool's CMake-Tutorial https://www.youtube.com/watch?v=SYgESCQeGJY&list=PLK6MXr8gasrGmIiSuVQXpfFuE1uPT615s&index=8.
Obviously, I am using a different CMake-Version than the one that he's using in the video. But before changing my version I want to tackle the problem here.
After configuring successfully via cmake .., I run make and receive the following:
[ 25%] Building CXX object say-hello/CMakeFiles/say-hello.dir/src/say-hello/hello.cpp.o
<command-line>:0:14: warning: ISO C++11 requires whitespace after the macro name
/home/maximilian/Dokumente/02_Programmieren/VSCMAKE/say-hello/src/say-hello/hello.cpp: In function ‘void hello::say()’:
<command-line>:0:17: error: expected ‘;’ before numeric constant
/home/maximilian/Dokumente/02_Programmieren/VSCMAKE/say-hello/src/say-hello/hello.cpp:7:43: note: in expansion of macro ‘HELLO_VERSION’
std::cout << "Hello (Version " << HELLO_VERSION << ")\n";
^~~~~~~~~~~~~
say-hello/CMakeFiles/say-hello.dir/build.make:62: recipe for target 'say-hello/CMakeFiles/say-hello.dir/src/say-hello/hello.cpp.o' failed
make[2]: *** [say-hello/CMakeFiles/say-hello.dir/src/say-hello/hello.cpp.o] Error 1
CMakeFiles/Makefile2:90: recipe for target 'say-hello/CMakeFiles/say-hello.dir/all' failed
make[1]: *** [say-hello/CMakeFiles/say-hello.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
I am using:
Linux
g++ 8.1
C++11
CMake 3.14.4
Visual Studio Code
Top CMakeLists.txt
cmake_minimum_required(VERSION 3.14.4)
project(MyProject VERSION 1.0.0)
set(CMAKE_CXX_STANDARD 11)
add_subdirectory(say-hello)
add_subdirectory(mainsrc)
CMakeLists.txt in mainsrc
add_executable(main.out main.cpp)
target_link_libraries(main.out PRIVATE say-hello)
main.cpp
#include <iostream>
#include <say-hello/hello.hpp>
int main(){
hello::say();
return 0;
}
CMakeLists.txt in say-hello
add_library(say-hello
src/say-hello/hello.cpp
src/say-hello/hello.hpp
)
target_include_directories(say-hello PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/src")
target_compile_definitions(say-hello PUBLIC HELLO_VERSION-4)
hello.hpp
#ifndef HELLO
#define HELLO
//#define SAYHELLOVERSION 4
namespace hello{
void say();
}
#endif
hello.cpp
#include <iostream>
#include "hello.hpp"
namespace hello{
void say(){
//int version = SAYHELLOVERSION;
std::cout << "Hello (Version " << HELLO_VERSION << ")\n";
}
}

cmake linking error 2 [duplicate]

This question already has answers here:
Error with multiple definitions of function
(4 answers)
Closed 4 years ago.
I am trying to compile a simple code using cmake and I getting an error. The code and cmake file are as below. The test.cpp is the main file in which i have directly included test1.cpp. I have also included my CMake file and the error that I am getting on performing make.
test.cpp
#ifndef _IOSTREAM_
#include<iostream>
#endif
#include"test1.cpp"
using namespace std;
int main()
{
printing("hello");
return 0;
}
test1.cpp
#ifndef _IOSTREAM_
#include<iostream>
#endif
#include<string>
using namespace std;
void printing(string s)
{
cout<<s<<endl;
return;
}
CMakeLists.txt
cmake_minimum_required(VERSION 2.6)
set(CMAKE_C_COMPILER "/usr/bin/clang")
set(CMAKE_CXX_COMPILER "/usr/bin/clang++")
project(test)
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -std=c++11)
add_executable(test test.cpp test1.cpp)
Error
CMakeFiles/test.dir/test1.cpp.o: In function
printing(std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >):
/home/vatsal/Desktop/test/test1.cpp:(.text+0x0): multiple definition
of printing(std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >)
CMakeFiles/test.dir/test.cpp.o:/home/vatsal/Desktop/test/test.cpp:
(.text+0x0): first defined here
clang: error: linker command failed with exit code 1 (use -v to see
invocation)
CMakeFiles/test.dir/build.make:98: recipe for target test failed
make[2]: *** [test] Error 1
CMakeFiles/Makefile2:67: recipe for target CMakeFiles/test.dir/all
failed
make[1]: *** [CMakeFiles/test.dir/all] Error 2
Makefile:83: recipe for target all failed
make: *** [all] Error 2
It happens because include cpp file is a bad idea.
After preprocessor work you'll get two defenition of void printing(string s) The first one is in test.cpp because you've incleded test1.cpp and the second one is in test1.cpp.
Solution is to create test1.h which contain declaration of function:
#include<iostream>
#include<string>
using namespace std;
void printing(string s);
Then fix test1.cpp:
#include "test1.h"
using namespace std;
void printing(string s)
{
cout<<s<<endl;
return;
}
And finaly replace #include"test1.cpp" with #include"test1.h" in test.cpp

socket.h fails to compile because of <winsock2.h> inclusion

i am using Netbeans 8.1 IDE with g++ compiler (cygwin) to
build a custome Socket class whose implementation is well defined
in socket.cpp, and declared in socket.h as shown below:
//socket.h
#ifndef SOCKET_H
#define SOCKET_H
#include <windows.h>
#include <winsock2.h> /*Code builds successfully if this line is commented. errors otherwise*/
class Socket {
public:
Socket(){std::cout << "Testing: Inside socket constructor ..." << endl;}
Socket(const Socket&);
virtual ~Socket();
Socket& operator=(Socket&);
void Close();
protected:
Socket(SOCKET s); /*Windows socket handle, defined in <winsock2.h>*/
SOCKET s_;
int* refCounter_;
private:
static bool Start();
static void End();
static int nofSockets_;
};
#endif /* SOCKET_H */
now, i test-drove it like this:
//main.cpp
#include "socket.h"
int main() {
Socket socket;
return 0;
}
Above code built successfully when winsock2.h is removed, error otherwise.
Honestly i am new to the netbeans IDE and i can't understand what am doing
wrong. I Dont know what the IDE requires of me either. i need your help to
debugg this please. Thanks. Here's the error:
cd 'C:\wamp\www\klandestine'
C:\cygwin64\bin\make.exe -f Makefile CONF=Debug clean
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .clean-conf
make[1]: Entering directory '/cygdrive/c/wamp/www/klandestine'
rm -f -r build/Debug
rm -f dist/Debug/Cygwin-Windows/klandestine.exe
make[1]: Leaving directory '/cygdrive/c/wamp/www/klandestine'
CLEAN SUCCESSFUL (total time: 656ms)
cd 'C:\wamp\www\klandestine'
C:\cygwin64\bin\make.exe -f Makefile CONF=Debug
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory '/cygdrive/c/wamp/www/klandestine'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/Cygwin-Windows/klandestine.exe
make[2]: Entering directory '/cygdrive/c/wamp/www/klandestine'
mkdir -p build/Debug/Cygwin-Windows
rm -f "build/Debug/Cygwin-Windows/main.o.d"
g++ -c -g -MMD -MP -MF "build/Debug/Cygwin-Windows/main.o.d" -o build/Debug/Cygwin-Windows/main.o main.cpp
In file included from /usr/include/w32api/winsock2.h:56:0,
from socket.h:12,
from main.cpp:14:
/usr/include/w32api/psdk_inc/_fd_types.h:100:2: warning: #warning "fd_set and associated macros have been defined in sys/types. This can cause runtime problems with W32 sockets" [-Wcpp]
#warning "fd_set and associated macros have been defined in sys/types. \
^
In file included from socket.h:12:0,
from main.cpp:14:
/usr/include/w32api/winsock2.h:995:123: error: conflicting declaration of C function 'int select(int, _types_fd_set*, _types_fd_set*, _types_fd_set*, PTIMEVAL)'
WINSOCK_API_LINKAGE int WSAAPI select(int nfds,fd_set *readfds,fd_set *writefds,fd_set *exceptfds,const PTIMEVAL timeout);
^
In file included from /usr/include/sys/types.h:68:0,
from /usr/include/pthread.h:14,
from /usr/lib/gcc/x86_64-pc-cygwin/5.3.0/include/c++/x86_64-pc-cygwin/bits/gthr-default.h:35,
from /usr/lib/gcc/x86_64-pc-cygwin/5.3.0/include/c++/x86_64-pc-cygwin/bits/gthr.h:148,
from /usr/lib/gcc/x86_64-pc-cygwin/5.3.0/include/c++/ext/atomicity.h:35,
from /usr/lib/gcc/x86_64-pc-cygwin/5.3.0/include/c++/bits/ios_base.h:39,
from /usr/lib/gcc/x86_64-pc-cygwin/5.3.0/include/c++/ios:42,
from /usr/lib/gcc/x86_64-pc-cygwin/5.3.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/5.3.0/include/c++/iostream:39,
from main.cpp:9:
/usr/include/sys/select.h:73:5: note: previous declaration 'int select(int, _types_fd_set*, _types_fd_set*, _types_fd_set*, timeval*)'
int select __P ((int __n, fd_set *__readfds, fd_set *__writefds,
^
make[2]: *** [nbproject/Makefile-Debug.mk:69: build/Debug/Cygwin-Windows/main.o] Error 1
make[2]: Leaving directory '/cygdrive/c/wamp/www/klandestine'
make[1]: *** [nbproject/Makefile-Debug.mk:60: .build-conf] Error 2
make[1]: Leaving directory '/cygdrive/c/wamp/www/klandestine'
make: *** [nbproject/Makefile-impl.mk:40: .build-impl] Error 2
BUILD FAILED (exit value 2, total time: 3s)

Socket programming in C++ in Ubuntu

I have tried to implement a program using sockets in Ubuntu 10.04. Here is the code:
#include <iostream>
#include <sys/types.h>
#include<netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <errno.h>
#include <exception>
using namespace std;
using std::exception;
int main(int argc,char *argv[]){
int sockethandle;
if ((sockethandle=socket(AF_INET,SOCK_STREAM,IPPROTO_IP))<0)
{
close(sockethandle);
exit(EXIT_FAILURE);
}
return 0;
}
But here are the compile errors:
usr/bin/make -f nbproject/Makefile-Debug.mk SUBPROJECTS= .build-conf
make[1]: Entering directory `/home/david/NetBeansProjects/socket'
/usr/bin/make -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/socket
make[2]: Entering directory `/home/david/NetBeansProjects/socket'
mkdir -p build/Debug/GNU-Linux-x86
rm -f build/Debug/GNU-Linux-x86/main.o.d
g++ -c -g -MMD -MP -MF build/Debug/GNU-Linux-x86/main.o.d -o build/Debug/GNU-Linux-x86/main.o main.cpp
main.cpp: In function ‘int main()’:
main.cpp:18: error: ‘EXIT_FAILURE’ was not declared in this scope
main.cpp:18: error: ‘exit’ was not declared in this scope
make[2]: *** [build/Debug/GNU-Linux-x86/main.o] Error 1
make[2]: Leaving directory `/home/david/NetBeansProjects/socket'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/home/david/NetBeansProjects/socket'
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 929ms)
How can this be fixed?
EXIT_FAILURE and exit() are defined in <stdlib.h> since you have not included this in your module, the compiler is pointing out that it does not know what these symbols mean.
Adding:
#include <stdlib.h>
should sort your problem out.