I've got this code snippet trying to use gtest Debug log:
#include <gtest/gtest.h>
#include <iostream>
using namespace std;
int DieInDebugElse1(int* sideeffect) {
if (sideeffect) *sideeffect = 1;
#ifndef NDEBUG
GTEST_LOG_(FATAL)<<"debug death inside DieInDebugElse12()";
#endif // NDEBUG
return 12;
}
TEST(a,c){
int sideeffect = 0;
EXPECT_DEBUG_DEATH(DieInDebugElse1(&sideeffect), "death");
}
Compile error shows:
error: ‘FATAL’ was not declared in this scope
GTEST_LOG(FATAL, "debug death inside DieInDebugElse12()");
^~~~~
m.cpp:57:15: note: suggested alternative: ‘FAIL’
GTEST_LOG(FATAL, "debug death inside DieInDebugElse12()");
^~~~~
FAIL
m.cpp:57:5: error: ‘GTEST_LOG’ was not declared in this scope
GTEST_LOG(FATAL, "debug death inside DieInDebugElse12()");
^~~~~~~~~
m.cpp:57:5: note: suggested alternative: ‘GTEST_LOG_’
GTEST_LOG(FATAL, "debug death inside DieInDebugElse12()");
^~~~~~~~~
GTEST_LOG_
scons: *** [m.o] Error 1
How to fix it?
Related
I am trying to catch an error in a C++ OpenCL host and it does not compile. This is the program:
#include "gpu/cldrive/libcldrive.h"
#include "gpu/cldrive/kernel_arg_value.h"
#include "gpu/cldrive/kernel_driver.h"
#include "gpu/clinfo/libclinfo.h"
#include "labm8/cpp/logging.h"
#include "labm8/cpp/status.h"
#include "labm8/cpp/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/strings/strip.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
#define CL_HPP_ENABLE_EXCEPTIONS
#define LOG_CL_ERROR(level, error) \
LOG(level) << "OpenCL exception: " << error.what() << ", error: " \
<< labm8::gpu::clinfo::OpenClErrorString(error.err());
namespace gpu {
namespace cldrive {
namespace {
// Attempt to build OpenCL program.
labm8::StatusOr<cl::Program> BuildOpenClProgram(
const std::string& opencl_kernel, const cl::Context& context,
const string& cl_build_opts) {
auto start_time = absl::Now();
try {
// Assemble the build options. We need -cl-kernel-arg-info so that we can
// read the kernel signatures.
string all_build_opts = "-cl-kernel-arg-info ";
absl::StrAppend(&all_build_opts, cl_build_opts);
labm8::TrimRight(all_build_opts);
cl::Program program;
// SPIR-V Compilation
if(opencl_kernel.substr( opencl_kernel.length() - 4 ) == ".spv") {
std::vector<char> cl_kernel(opencl_kernel.begin(), opencl_kernel.end());
program = cl::Program{context, cl_kernel};
// OPENCL:
} else {
program = cl::Program{context, opencl_kernel};
}
program.build(context.getInfo<CL_CONTEXT_DEVICES>(),
all_build_opts.c_str());
auto end_time = absl::Now();
auto duration = (end_time - start_time) / absl::Milliseconds(1);
LOG(INFO) << "clBuildProgram() with options '" << all_build_opts
<< "' completed in " << duration << " ms";
return program;
} catch (cl::Error e) {
LOG_CL_ERROR(WARNING, e);
return labm8::Status(labm8::error::Code::INVALID_ARGUMENT,
"clBuildProgram failed");
}
}
} // namespace
When building with bazel, it gives me the following error:
ERROR: /home/enrique/Escritorio/cldrive/gpu/cldrive/BUILD:315:1: Couldn't
build file gpu/cldrive/_objs/libcldrive/libcldrive.pic.o: C++
compilation of rule '//gpu/cldrive:libcldrive' failed (Exit 1) gcc
failed: error executing command /usr/bin/gcc -U_FORTIFY_SOURCE
-fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -g0 -O2 '-D_FORTIFY_SOURCE=1' -DNDEBUG -ffunction-sections ... (remaining 299
argument(s) skipped)
Use --sandbox_debug to see verbose messages from the sandbox In file
included from ./gpu/cldrive/libcldrive.h:21:0,
from gpu/cldrive/libcldrive.cc:16: ./third_party/opencl/cl.hpp:438:109: note: #pragma message: cl2.hpp:
CL_HPP_TARGET_OPENCL_VERSION is not defined. It will default to 220
(OpenCL 2.2) # pragma message("cl2.hpp: CL_HPP_TARGET_OPENCL_VERSION
is not defined. It will default to 220 (OpenCL 2.2)")
^ gpu/cldrive/libcldrive.cc: In function 'labm8::StatusOr<cl::Program>
gpu::cldrive::{anonymous}::BuildOpenClProgram(const string&, const
cl::Context&, const string&)': gpu/cldrive/libcldrive.cc:76:16: error:
'Error' in namespace 'cl' does not name a type } catch (cl::Error
e) {
^~~~~ gpu/cldrive/libcldrive.cc:77:27: error: 'e' was not declared in this scope
LOG_CL_ERROR(WARNING, e);
^ gpu/cldrive/libcldrive.cc:34:41: note: in definition of macro 'LOG_CL_ERROR' LOG(level) << "OpenCL exception:
" << error.what() << ", error: " \
^~~~~ gpu/cldrive/libcldrive.cc: In member function 'void
gpu::cldrive::Cldrive::RunOrDie(gpu::cldrive::Logger&)':
gpu/cldrive/libcldrive.cc:93:16: error: 'Error' in namespace 'cl' does
not name a type } catch (cl::Error error) {
^~~~~ gpu/cldrive/libcldrive.cc:95:41: error: 'error' was not declared in this scope
<< " Raised by: " << error.what() << '\n'
^~~~~ gpu/cldrive/libcldrive.cc:95:41: note: suggested alternative: In file
included from ./gpu/cldrive/logger.h:21:0,
from ./gpu/cldrive/libcldrive.h:18,
from gpu/cldrive/libcldrive.cc:16: ./labm8/cpp/status.h:41:11: note: 'labm8::error' namespace error {
^~~~~
In the class header (libcldrive.h) the header cl2.hpp is included. This code worked with old headers, it began giving error when updating the old cl.hpp to cl2.hpp. I look for the class in the new header and it still exists, so i do not understand why it is giving error.
Thanks in advance.
Hans has already linked to another question about the same issue in the comments, but although correct, I don't find the answer there particularly complete, helpful, or friendly.
Essentially, raising exceptions on OpenCL errors is an optional feature of the C++ wrapper. (exceptions in C++ are controversial to say the least) So it needs to be explicitly enabled using the CL_HPP_ENABLE_EXCEPTIONS preprocessor macro, before #includeing the wrapper header.
This is documented in the C++ wrapper's documentation here.
The documentation also gives a helpful example, where this feature is enabled:
#define CL_HPP_ENABLE_EXCEPTIONS
#define CL_HPP_TARGET_OPENCL_VERSION 200
#include <CL/cl2.hpp>
Note that your build is also warning you about not defining CL_HPP_TARGET_OPENCL_VERSION - you probably should do that to match your OpenCL headers' version.
I am new in C++ development , As I learn online about extern variable - I tried it to string variables and its working fine.But I have to work with string variable as its not working.Please look as follow.
globals.h
#include <iostream>
using namespace std;
#ifndef SIMULATIONFILEPARSER_GLOBALS_H
#define SIMULATIONFILEPARSER_GLOBALS_H
//sequence of files to be execute
extern string ipFiles[];
#endif //SIMULATIONFILEPARSER_GLOBALS_H
globals.cpp
#include "../headers/globals.h"
//sequence of files to be execute
string ipFiles[] = {"in.relaxSubstrate", "in.relaxFluid"};
main.cpp
#include <iostream>
#include "Source/headers/globals.h"
int main() {
for (string &ipFileName :ipFiles) {
std::cout << ipFileName << std::endl;
}
return 0;
}
When I try to run this project , It gives following error
C:\Users\king\ClionProjects\SimulationFileParser\main.cpp: In function 'int main()':
C:\Users\king\ClionProjects\SimulationFileParser\main.cpp:5:30: error: range-based 'for' expression of type 'std::__cxx11::basic_string<char> []' has incomplete type
for (string &ipFileName :ipFiles) {
^
CMakeFiles\SimulationFileParser.dir\build.make:61: recipe for target 'CMakeFiles/SimulationFileParser.dir/main.cpp.obj' failed
mingw32-make.exe[3]: *** [CMakeFiles/SimulationFileParser.dir/main.cpp.obj] Error 1
mingw32-make.exe[3]: *** Waiting for unfinished jobs....
mingw32-make.exe[2]: *** [CMakeFiles/SimulationFileParser.dir/all] Error 2
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/SimulationFileParser.dir/all' failed
mingw32-make.exe[1]: *** [CMakeFiles/SimulationFileParser.dir/rule] Error 2
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/SimulationFileParser.dir/rule' failed
mingw32-make.exe: *** [SimulationFileParser] Error 2
Makefile:117: recipe for target 'SimulationFileParser' failed
The compiler doesn't complain about a symbol, that's not visible. It's telling you, that the type is incomplete:
range-based 'for' expression of type 'std::__cxx11::basic_string []' has incomplete type
Until the compiler knows the size of the array, it cannot compile the range-based for loop. To change this, you need to declare a complete type. This could be an array with an explicit size, or - and that's the recommended solution - a standard container1:
globals.h
#pragma once
#include <string>
#include <vector>
extern std::vector<std::string> ipFiles;
globals.cpp
std::vector<std::string> ipFiles{"in.relaxSubstrate", "in.relaxFluid"};
You don't have to change main.cpp. But if you want to make it fancy, you could use auto as well as exercising const-correctness:
int main() {
for (const auto& ipFileName : ipFiles) {
std::cout << ipFileName << std::endl;
}
return 0;
}
1 The size of a standard container does not need to be known at compile time. All the compiler needs are the (forward) iterators to begin() and end() of the controlled sequence. An array, on the other hand, doesn't provide those as member functions, and the compiler needs to generate them. It needs to know the size to generate the equivalent of end().
This loop does not know the size of array.
for (string &ipFileName :ipFiles)
Change in global.h
extern string ipFiles[2];
change in global.cpp
string ipFiles[2] = {"in.relaxSubstrate", "in.relaxFluid"};
After this your code should compile properly.
I'm trying to use Eigen3 to compute some complex eigenvalues and sofar everything worked, but know I get a weird error:
g++ -I/usr/include/eigen3/ -Icubature/ -I/usr/include/boost/ cubature/hcubature.c main.cpp wavefunction.cpp -o main.x
main.cpp: In function ‘int main(int, char**)’:
main.cpp:103:2: error: ‘ComplexEigenSolver’ was not declared in this scope
ComplexEigenSolver<MatrixXcd> ces;
^~~~~~~~~~~~~~~~~~
main.cpp:103:2: note: suggested alternative:
In file included from /usr/include/eigen3/Eigen/Eigenvalues:35:0,
from /usr/include/eigen3/Eigen/Dense:7,
from main.cpp:4:
/usr/include/eigen3/Eigen/src/Eigenvalues/ComplexEigenSolver.h:45:38: note: ‘Eigen::ComplexEigenSolver’
template<typename _MatrixType> class ComplexEigenSolver
^~~~~~~~~~~~~~~~~~
main.cpp:103:30: error: expected primary-expression before ‘>’ token
ComplexEigenSolver<MatrixXcd> ces;
^
main.cpp:103:32: error: ‘ces’ was not declared in this scope
ComplexEigenSolver<MatrixXcd> ces;
^~~
make: *** [Makefile:2: all] Error 1
Here are my includes:
#include <iostream>
#include <vector>
#include <Eigen/Core>
#include <Eigen/Dense>
#include <Eigen/Eigenvalues>
#include "wavefunction.cpp"
#include "cubature.h"
using Eigen::MatrixXcd;
This is the routine where the error occurs:
MatrixXcd hop(5,5);
for(int m1 = -2; m1 <= 2; m1++){
for(int m2 = -2; m2 <= 2; m2++){
hop(m1+2, m2+2) = ....;
}
}
ComplexEigenSolver<MatrixXcd> ces;
Everything works fine, only the last line doesn't work. I don't see the difference between my code and the example given at the Eigen website.
What am I doing wrong?
I am new in C++ development , As I learn online about extern variable - I tried it to string variables and its working fine.But I have to work with string variable as its not working.Please look as follow.
globals.h
#include <iostream>
using namespace std;
#ifndef SIMULATIONFILEPARSER_GLOBALS_H
#define SIMULATIONFILEPARSER_GLOBALS_H
//sequence of files to be execute
extern string ipFiles[];
#endif //SIMULATIONFILEPARSER_GLOBALS_H
globals.cpp
#include "../headers/globals.h"
//sequence of files to be execute
string ipFiles[] = {"in.relaxSubstrate", "in.relaxFluid"};
main.cpp
#include <iostream>
#include "Source/headers/globals.h"
int main() {
for (string &ipFileName :ipFiles) {
std::cout << ipFileName << std::endl;
}
return 0;
}
When I try to run this project , It gives following error
C:\Users\king\ClionProjects\SimulationFileParser\main.cpp: In function 'int main()':
C:\Users\king\ClionProjects\SimulationFileParser\main.cpp:5:30: error: range-based 'for' expression of type 'std::__cxx11::basic_string<char> []' has incomplete type
for (string &ipFileName :ipFiles) {
^
CMakeFiles\SimulationFileParser.dir\build.make:61: recipe for target 'CMakeFiles/SimulationFileParser.dir/main.cpp.obj' failed
mingw32-make.exe[3]: *** [CMakeFiles/SimulationFileParser.dir/main.cpp.obj] Error 1
mingw32-make.exe[3]: *** Waiting for unfinished jobs....
mingw32-make.exe[2]: *** [CMakeFiles/SimulationFileParser.dir/all] Error 2
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/SimulationFileParser.dir/all' failed
mingw32-make.exe[1]: *** [CMakeFiles/SimulationFileParser.dir/rule] Error 2
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/SimulationFileParser.dir/rule' failed
mingw32-make.exe: *** [SimulationFileParser] Error 2
Makefile:117: recipe for target 'SimulationFileParser' failed
The compiler doesn't complain about a symbol, that's not visible. It's telling you, that the type is incomplete:
range-based 'for' expression of type 'std::__cxx11::basic_string []' has incomplete type
Until the compiler knows the size of the array, it cannot compile the range-based for loop. To change this, you need to declare a complete type. This could be an array with an explicit size, or - and that's the recommended solution - a standard container1:
globals.h
#pragma once
#include <string>
#include <vector>
extern std::vector<std::string> ipFiles;
globals.cpp
std::vector<std::string> ipFiles{"in.relaxSubstrate", "in.relaxFluid"};
You don't have to change main.cpp. But if you want to make it fancy, you could use auto as well as exercising const-correctness:
int main() {
for (const auto& ipFileName : ipFiles) {
std::cout << ipFileName << std::endl;
}
return 0;
}
1 The size of a standard container does not need to be known at compile time. All the compiler needs are the (forward) iterators to begin() and end() of the controlled sequence. An array, on the other hand, doesn't provide those as member functions, and the compiler needs to generate them. It needs to know the size to generate the equivalent of end().
This loop does not know the size of array.
for (string &ipFileName :ipFiles)
Change in global.h
extern string ipFiles[2];
change in global.cpp
string ipFiles[2] = {"in.relaxSubstrate", "in.relaxFluid"};
After this your code should compile properly.
I'm learning C++ and I'm at the point of using the <algorithm> header and I get this compiler error even with an empty project:
/Users/italrolando/hpc-gcc47/bin/../lib/gcc/x86_64-apple-darwin11.4.0/4.7.1/../../../../include/c++/4.7.1/cmath:1108:11: error: '::llrint' has not been declared
/Users/italrolando/hpc-gcc47/bin/../lib/gcc/x86_64-apple-darwin11.4.0/4.7.1/../../../../include/c++/4.7.1/cmath:1109:11: error: '::llrintf' has not been declared
/Users/italrolando/hpc-gcc47/bin/../lib/gcc/x86_64-apple-darwin11.4.0/4.7.1/../../../../include/c++/4.7.1/cmath:1110:11: error: '::llrintl' has not been declared
/Users/italrolando/hpc-gcc47/bin/../lib/gcc/x86_64-apple-darwin11.4.0/4.7.1/../../../../include/c++/4.7.1/cmath:1112:11: error: '::llround' has not been declared
/Users/italrolando/hpc-gcc47/bin/../lib/gcc/x86_64-apple-darwin11.4.0/4.7.1/../../../../include/c++/4.7.1/cmath:1113:11: error: '::llroundf' has not been declared
/Users/italrolando/hpc-gcc47/bin/../lib/gcc/x86_64-apple-darwin11.4.0/4.7.1/../../../../include/c++/4.7.1/cmath:1114:11: error: '::llroundl' has not been declared
make: *** [algorithm.o] Error 1
Here is the code:
#include <iostream>
#include <algorithm>
using namespace std;
int main( int argc, char ** argv ){
return 0;
}
I'm using Eclipse with GCC 4.7.1 compiler and it's quite strange since the course I'm following is using Eclipse with Gcc 4.7 and it works.
Thanks