I want to expose generic Timer
. The problem is I don't know why extern
got underlined with red and the Visual Studio 2019 Community says "linking specification is not allowed".
Question
What is the correct syntax for extern
?
Minimal Working Example
The header utilities.hpp
:
#pragma once
#ifdef UTILITIES_EXPORTS
#define UTILITIES_API __declspec(dllexport)
#else
#define UTILITIES_API __declspec(dllimport)
#endif
namespace Utilities
{
template<typename F>
extern "C" UTILITIES_API void Timer(F f, unsigned int N = 1);
}
The definition file utilities.cpp
:
#include "Timer.h"
#include <iostream>
#include <vector>
#include <chrono>
#include <iomanip>
using namespace std;
namespace Utilities
{
template<typename F>
void Timer(F f, unsigned int N)
{
cout << fixed << setprecision(9);
vector<unsigned int> results;
const double million = 1'000'000'000.0;
for (unsigned int i = 0; i < N; i++)
{
chrono::steady_clock::time_point begin = chrono::steady_clock::now();
f();
chrono::steady_clock::time_point end = chrono::steady_clock::now();
unsigned int interval = chrono::duration_cast<chrono::nanoseconds>(end - begin).count();
results.push_back(interval);
double elapsedTime = interval / million;
cout << "Elapsed: \t\t" << elapsedTime << " s." << endl;
}
unsigned long long sum = 0;
for (unsigned int x : results)
sum += x;
double totalElapsedTime = sum / million / results.size();
cout << "\t\t\tAverage elapsed: " << totalElapsedTime << " s." << endl;
}
}