No template named 'function' - c++

I'm trying to compile on mac a code written in C++ and there is a function
tuple<string, string, int> getURL(const string& url, const function<void(CURL*)>& opts)
{...}
with an error
no template named 'function'
...int> getURL(const string& url, const funct...
^
I've tried to modify the functions header. After that I could compile that module, but the compiler couldn't link with the others:
"getURL(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::function<void (void*)> const&)", referenced from:
CensysScanner::getHostInfo(Host*) in CensysScanner.cpp.o
DebianLookup::FindVulnerability(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, OpSys, double) in DebianLookup.cpp.o
DebianLookup::GetChangelog(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, OpSys, double) in DebianLookup.cpp.o
EnterpriseLinuxLookup::FindVulnerability(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, OpSys, double) in EnterpriseLinuxLookup.cpp.o
EnterpriseLinuxLookup::GetChangelog(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, OpSys, double) in EnterpriseLinuxLookup.cpp.o
LooquerScanner::getHostInfo(Host*) in LooquerScanner.cpp.o
ShodanScanner::getHostInfo(Host*) in ShodanScanner.cpp.o
...
I've also included functional module (#include <functional>) and I'm using std namespace (using namespace std;).

"getURL(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::function<void (void*)> const&)", referenced from:
This symbol the other object files reference has as parameter a std::__1::function. This is std::function from the standard library, which function in your first code block is apparently supposed to refer to.
Check that #include<functional> is included, that you are compiling with C++11 or later and write std::function instead of function or check that you have a using statement importing function from std into the current namespace, e.g. using namespace std; or using std::function;.

With std::function works. I don't know why needed the std:: because at the top of the file the namespace was told (using namespace std;), but it works.

Related

xcode : undefined symbols when including headers

I recently started a project using swift and c++. So I installed Xcode and started using it. I first encountered some bugs, but fixed theme.
However, I still got some Undefined symbols for architecture x86_64:
I searched online and I can't find the solution for my problem (otherwise I would not have asked the question).
This is the whole error :
"parser(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >, Node*, int, int)", referenced from:
utilise_file(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, bool, bool, bool, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in main.o
"recognize_paternes(Node*, bool)", referenced from:
utilise_file(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, bool, bool, bool, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in main.o
"ast_t_rep(Node*, int)", referenced from:
utilise_file(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, bool, bool, bool, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in main.o
"lex(char const*, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >&)", referenced from:
utilise_file(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, bool, bool, bool, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in main.o
"visitor(std::__1::vector<Node*, std::__1::allocator<Node*> >, bool, bool, bool)", referenced from:
utilise_file(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, bool, bool, bool, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
But I think we can just consider :
utilise_file(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, bool, bool, bool, bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in main.o
as this error seems to be redundant.
In my main.cpp file, I imported the header file (parser.h) where I declared Node *parser(vector<string>, Node *, int, int);.
In main.cpp>utilise_file(...), I call the function parser:
void utilise_file(){
...
vector<string> code_ref = vector<string>();
vector<string> lexeme = lex(source.c_str(), code_ref);
Node *ast_t = new Node();
ast_t->value = "main";
ast_t->type = "root";
Node *p = parser(lexeme, ast_t, 0, int(lexeme.size()));
...
}
So normally, I would not get this error.
It seems to me that it is a 'type' sort of problem, but I can't figure what.
I searched online for solutions, and I tried adding -Xlinker in Xcode>Linking>Other Liner Flags, but it didn't helped.
I also thought of adding the flags linking to the header in the compiling command line, but I think Xcode does that automatically. (Or not because many of my headers are not recognized in the file arborescence...)
Thank you for your help ;)
Edit : I found the problem, I had to manually add the previously written header files in Xcode (which did not do it automatically).
I found the problem.
When I imported the previously written header files in Xcode, Xcode did not attached them to the project.
I had to manually add theme, and now it works fine !

clang: error: linker command failed with exit code 1 (use -v to see invocation) *about global variables

I have a problem with compiling my files.
I compiled and got this message
"Undefined symbols for architecture x86_64:
"_airport_label", referenced from:
FlightMap::pathfind_before(std::__1::basic_string<char,
std::__1::char_traits<char>, std::__1::allocator<char> > const&,
std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> > const&) in FlightMap-9a7ab0.o
FlightMap::pathfind(std::__1::basic_string<char,
std::__1::char_traits<char>, std::__1::allocator<char> > const&,
std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> > const&) in FlightMap-9a7ab0.o
"_dijkstra", referenced from:
FlightMap::findShortestRoute(std::__1::basic_string<char,
std::__1::char_traits<char>, std::__1::allocator<char> > const&,
std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> > const&) in FlightMap-9a7ab0.o
FlightMap::pathfind_before(std::__1::basic_string<char,
std::__1::char_traits<char>, std::__1::allocator<char> > const&,
std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> > const&) in FlightMap-9a7ab0.o
FlightMap::fun_dijkstra(std::__1::basic_string<char,
std::__1::char_traits<char>, std::__1::allocator<char> > const&) in FlightMap-9a7ab0.o
(maybe you meant: __ZN9FlightMap12fun_dijkstraERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEE)
"_distance_label", referenced from:
FlightMap::pathfind_before(std::__1::basic_string<char,
std::__1::char_traits<char>, std::__1::allocator<char> > const&,
std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> > const&) in FlightMap-9a7ab0.o
FlightMap::pathfind(std::__1::basic_string<char,
std::__1::char_traits<char>, std::__1::allocator<char> > const&,
std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> > const&) in FlightMap-9a7ab0.o
"_path", referenced from:
FlightMap::pathfind(std::__1::basic_string<char,
std::__1::char_traits<char>, std::__1::allocator<char> > const&,
std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> > const&) in FlightMap-9a7ab0.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)"
I think there are some problems in global variable in FlightMap.h file.
(I edited Flight.h -> FlightMap.h)
I declared 4 global variables like
stack<string> path;
map<string, string > airport_label;
map<double, string > distance_label;
map<string, double > dijkstra;
I have already tried to put "extern" in front of each global variable but it does not work.
In FlightMap.h file code(Edited Flight.h -> FlightMap.h)
#ifndef LAB6_FLIGHTMAP_H
#define LAB6_FLIGHTMAP_H
#include <iostream>
#include <string>
#include <stdexcept>
#include <map>
#include <list>
#include <vector>
#include <stack>
#include "AdjacencyListDirectedGraph.h"
extern stack<string> path;
extern map<string, string > airport_label;
extern map<double, string > distance_label;
extern map<string, double > dijkstra;
.
.
.
Also, those global variables are used in FlightMap.cpp file.(I edited!)
I think that is where problems occur
Use fully qualified names in your header file's declarations:
#ifndef LAB6_FLIGHTMAP_H
#define LAB6_FLIGHTMAP_H
//#includes ...
extern std::stack<std::string> path;
extern std::map<std::string, std::string> airport_label;
extern std::map<double, std::string> distance_label;
extern std::map<std::string, double> dijkstra;
...
#endif
And in FlightMap.cpp you need to define these global variabes:
std::stack<std::string> path;
std::map<std::string, std::string> airport_label;
std::map<double, std::string> distance_label;
std::map<std::string, double> dijkstra;
extern tells the compiler that the variable will be defined somewhere, in another translation unit, and leaves resolving this up to the linker. The linker now has to find the definition in any of the compiled files, but can't since there is no definition anywhere. By actually putting the definition (that is, without extern) in one file (and one file only), the linker will be happy.

Error reading a Trajectory File using Chemfiles library

I need to write a piece of cpp code that will read a trajectory file (.pdb or .dcd files). I have followed the steps on the homepage of chemfiles library to read in the file. I am getting an error about undefined symbols for architecture x86_64 (please see a piece of the error log below).
What I want: Able to read the .dcd/.pdb file using the chemfiles library (or any other suitable library to do so) - I need to access the coordinates of the atoms listed in the file.
Code that reproduces the error when compiled using the command below:
#include "/usr/local/include/chemfiles.hpp" //path to library on my mac
int main(int argc, char *argv[]) {
chemfiles::Trajectory testfile("test.dcd");
return 1;
}
Code that compiles successfully in CLI using the command below:
#include "/usr/local/include/chemfiles.hpp" //path to library on my mac
int main(int argc, char *argv[]) {
// chemfiles::Trajectory testfile("test.dcd");
return 1;
}
Command to compile program:
g++-8 -fopenmp trial.cpp -o trial -lchemfiles -L /usr/local/lib
// I am using openmp because my original code is about parallelisation with openmp
Error:
Undefined symbols for architecture x86_64:
"chemfiles::Trajectory::Trajectory(std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> >, char,
std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> > const&)", referenced from:
_main in ccnk7agz.o
"std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> >::find(char, unsigned long) const",
referenced from:
file_open_info::parse(std::__1::basic_string<char,
std::__1::char_traits<char>, std::__1::allocator<char> > const&,
std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> > const&) in libchemfiles.a(Trajectory.cpp.o)
split_comment(std::__1::basic_string<char,
std::__1::char_traits<char>, std::__1::allocator<char> >&) in
libchemfiles.a(LAMMPSData.cpp.o)
"std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> >::rfind(char, unsigned long) const",
referenced from:
file_open_info::parse(std::__1::basic_string<char,
std::__1::char_traits<char>, std::__1::allocator<char> > const&,
std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> > const&) in libchemfiles.a(Trajectory.cpp.o)
chemfiles::SDFFormat::read(chemfiles::Frame&) in
libchemfiles.a(SDF.cpp.o)
"std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> >::compare(unsigned long, unsigned long, char
const*, unsigned long) const", referenced from:
file_open_info::parse(std::__1::basic_string<char,
std::__1::char_traits<char>, std::__1::allocator<char> > const&,
std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> > const&) in libchemfiles.a(Trajectory.cpp.o)
chemfiles::Trajectory::Trajectory(std::__1::basic_string<char,
std::__1::char_traits<char>, std::__1::allocator<char> >, char,
std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> > const&) in libchemfiles.a(Trajectory.cpp.o)
chemfiles::FormatFactory::register_format(chemfiles::FormatInfo,
std::__1::function<std::__1::unique_ptr<chemfiles::Format,
std::__1::default_delete<chemfiles::Format> >
(std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> >, chemfiles::File::Mode,
chemfiles::File::Compression)>) in libchemfiles.a(FormatFactory.cpp.o)
chemfiles::Frame::guess_bonds() in
libchemfiles.a(Frame.cpp.o)
chemfiles::FormatInfo::FormatInfo(std::__1::basic_string<char,
std::__1::char_traits<char>, std::__1::allocator<char> >) in
libchemfiles.a(CSSR.cpp.o)
chemfiles::FormatInfo::FormatInfo(std::__1::basic_string<char,
std::__1::char_traits<char>, std::__1::allocator<char> >) in
libchemfiles.a(MMTF.cpp.o)
chemfiles::MOL2Format::read(chemfiles::Frame&) in
libchemfiles.a(MOL2.cpp.o)
...

C++: Classic undefined symbols for architecture error

Learning C++ lately, and I'm having trouble to compile my classes. I am currently getting the following obscure template error:
Undefined symbols for architecture x86_64:
"SmallWorld::AJV::validate(nlohmann::basic_json<std::__1::map, std::__1::vector, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, bool, long long, unsigned long long, double, std::__1::allocator, nlohmann::adl_serializer>*, nlohmann::basic_json<std::__1::map, std::__1::vector, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, bool, long long, unsigned long long, double, std::__1::allocator, nlohmann::adl_serializer>*)", referenced from:
SmallWorld::Map::(anonymous namespace)::readMap(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in map.test.cpp.o
"std::__1::function<unsigned long (SmallWorld::Map::Graph<SmallWorld::Region>)> SmallWorld::Map::algorithm::dfs<SmallWorld::Region>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::function<bool (std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::set<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::less<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >&, std::__1::unordered_map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::hash<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::equal_to<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > > > const&)> const&)", referenced from:
creates_a_connected_graph_small_world_map_Test::TestBody() in map.test.cpp.o
"SmallWorld::Region::Region(nlohmann::basic_json<std::__1::map, std::__1::vector, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, bool, long long, unsigned long long, double, std::__1::allocator, nlohmann::adl_serializer>)", referenced from:
std::__1::shared_ptr<SmallWorld::Region> std::__1::shared_ptr<SmallWorld::Region>::make_shared<nlohmann::basic_json<std::__1::map, std::__1::vector, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, bool, long long, unsigned long long, double, std::__1::allocator, nlohmann::adl_serializer> const&>(nlohmann::basic_json<std::__1::map, std::__1::vector, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, bool, long long, unsigned long long, double, std::__1::allocator, nlohmann::adl_serializer> const&&&) in map.test.cpp.o
ld: symbol(s) not found for architecture x86_64
which can be simplified to:
Undefined symbols for architecture x86_64:
"SmallWorld::AJV::validate(nlohmann::json*, nlohmann::json*)", referenced from:
SmallWorld::Map::(anonymous namespace)::readMap(string const&, string const&) in map.test.cpp.o
"std::function<size_t(Graph<Region>)> SmallWorld::Map::algorithm::dfs<SmallWorld::Region>(string const&, std::function<bool (string const&, std::set<string> const&, std::unordered_map<string, string> const&)", referenced from:
creates_a_connected_graph_small_world_map_Test::TestBody() in map.test.cpp.o
"SmallWorld::Region::Region(nlohmann::json)", referenced from:
std::shared_ptr<SmallWorld::Region> std::shared_ptr<SmallWorld::Region>::make_shared<nlohmann::json const&&&) in map.test.cpp.o
ld: symbol(s) not found for architecture x86_64
Which to me seems like it is saying that the method bool AJV::validate(json* schema, json* data) does not exist on type AJV. However I have verified the common mistakes (different implementation from header declaration, different invocation from implementation), to no avail.
Here is the definition and implementation of the AJV class:
// AJV.h
#ifndef SMALLWORLD_AJV_H
#define SMALLWORLD_AJV_H
#include <nlohmann/json.hpp>
using nlohmann::json;
namespace SmallWorld {
class AJV {
public:
std::function<bool(json*)> compile(json* schema);
bool validate(json* schema, json* data);
json errors;
private:
json m_schema;
std::function<bool(json*)> m_validator;
};
};
#endif // SMALLWORLD_AJV_H
// AJV.cpp
#include <nlohmann/json.hpp>
#include "AJV.h"
using nlohmann::json;
namespace SmallWorld {
std::function<bool(json*)> AJV::compile(json* schema) {
return [](json* data){ return true; };
};
bool AJV::validate(json* schema, json* data){ return true; };
};
And here is the invocation:
//loader.cpp
json* readMap(const string& map_path, const string& schema_path) {
AJV ajv;
json* schema = readJSONFile(schema_path);
json* jmap = readJSONFile(map_path);
if(ajv.validate(schema, jmap)){
delete schema;
return jmap;
}else{
delete schema;
throw ajv.errors;
}
};
Setup:
OS: MacOS 10.13.3
C++ Version: 14
Compiler: g++ => Apple LLVM version 9.0.0 (clang-900.0.39.2)
Build manager: CMake 3.5.1
IDE: Atom IDE w/ linter-clang for linting (not really an IDE XD)
Additional Details
Compilation is done via command line: mkdirp build && cd build && cmake .. && make
File AJV.h is within source tree at the specified include path.
How can I solve this problem?
Cheers ☀️
When you register a project in CMake, you list both headers and source files. It will then deal with these as best it can for the respective IDE / build system that you tell it to use.
If you always have one header and one cpp file (there's no requirement to; you may find some objects are complex/large enough that you want to split it into multiple cpp files for your sanity) then you could list all your file names with no extension in the cmake and have it append the .cxx and .h to the end for you. This will leave you with just half the typing.
As an alternative there is a non-recommended route which is to to use the file command to scan the directory for files - but this is done only when cmake is run; and as such will not auto-update; leaving it possible to have a source tree that's building fine on one system; while not on another.

Getting "undefined symbols for architecture" error when separating some functions from main.cpp, what to do?

The story
My main.cpp is getting really cluttered so I decided to separate other functions from it (some helper functions for IO operations in main()).
The problem
The files compile fine when the functions are within main.cpp. When I put them to a .h - .cpp pair, I get the following compiler error.
The line I use:
g++ -I ./headers/ ./definitions/*.cpp -o main.o main.cpp
The error:
Undefined symbols for architecture x86_64:
"void showSettings<double>(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> >,
std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> > > > const&,
std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >,
std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> > > > const&, int, int, double, double, int, int, int, int, int)",
referenced from:
_main in main-7k1oIa.o
ld: symbol(s) not found for architecture x86_64
The only working solution right now is to bring the functions back to main.cpp. I've included all necessary headers, using namespace std and prepending std:: to vector and string but the error keeps showing.
If it's of any help, the code that appears in the error is:
template <typename inputType>
void showSettings( const vector<string> &a,
const vector<string> &b,
int eq,
int meth,
inputType root1,
inputType root2,
int sigs,
int showPerLoop,
int plotRoots,
int loopMode,
int minLoops)
Templates are only instantiated when called, so if no function in your secondary .cpp file calls showSettings with a <double>, then the function wasn't created.
Generally, you want to leave template functions in a .h file so that the actual definition is visible when needed.
(See also: Why should the implementation and the declaration of a template class be in the same header file?)