相关疑难解决方法(0)

如何解决错误LNK2019:未解析的外部符号 - 功能?

我收到此错误,但我不知道如何修复它.

我正在使用Visual Studio 2013.我制作了解决方案名称MyProjectTest 这是我的测试解决方案的结构:

结构

- function.h

#ifndef MY_FUNCTION_H
#define MY_FUNCTION_H

int multiple(int x, int y);
#endif
Run Code Online (Sandbox Code Playgroud)

-function.cpp

#include "function.h"

int multiple(int x, int y){
    return x*y;
}
Run Code Online (Sandbox Code Playgroud)

- main.cpp

#include <iostream>
#include <cstdlib>
#include "function.h"
using namespace std;

int main(){
    int a, b;
    cin >> a >> b;
    cout << multiple(a, b) << endl;

    system("pause");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我是初学者; 这是一个简单的程序,它运行没有错误.我在互联网上阅读并对单元测试感兴趣,所以我创建了一个测试项目:

文件>新建>项目...>已安装>模板> Visual C++>测试>本机单元测试项目>

名称:UnitTest1 解决方案:添加到解决方案 然后位置自动切换到当前打开解决方案的路径这是解决方案的文件夹结构:

文件夹结构

我只编辑了文件unittest1.cpp:

#include "stdafx.h"
#include "CppUnitTest.h"
#include "../MyProjectTest/function.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework; …
Run Code Online (Sandbox Code Playgroud)

c++ testing error-handling lnk2019

90
推荐指数
6
解决办法
41万
查看次数

C++命名空间内的extern"C"链接?

namespace someNameSpace {
    extern "C" void doSomething()
        {
             someOperations();
        }
}
Run Code Online (Sandbox Code Playgroud)

我想doSomething()在C++和C环境中运行.

someNameSpace仍然封装doSomething(),如果我将其暴露在extern "C"联动?

有没有一种很好的方法在C++和C之间共享函数,同时避免在C++端污染全局命名空间?

编辑:因为此代码主要用于C++模式,而C链接仅供测试使用,我想这是一种更好的方法.

namespace someNameSpace {
    #ifdef COMPILE_FOR_C_LINKAGE
    extern "C"
    #else
    extern "C++"
    #endif
    { 
        void doSomething()
            {
                 someOperations();
            }
    }
}
Run Code Online (Sandbox Code Playgroud)

c c++ namespaces

20
推荐指数
2
解决办法
9988
查看次数

标签 统计

c++ ×2

c ×1

error-handling ×1

lnk2019 ×1

namespaces ×1

testing ×1