C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用c中用c编写的函数大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发一个项目,其中有些人已经用C语言编写代码,我们必须在C代码中使用它.所以我尝试按照测试编写一个测试程序来演示相同的:

文件是:

#ifndef h_files_n
    #define h_files_n

    #include<iostream>

    #ifdef __cplusplus
        extern "C" {
    #endif

    void add_func(int,int);

    #ifdef __cplusplus
        }
    #endif

    #endif

cpp文件是:

#include"h_files.h"

void add_func(int num,int nums)
{
    std :: cout << "The addition of numbers is : "<< num+nums << std endl;
}

c文件是:

#include<stdio.h>
#include"h_files.h"

int main()
{
    printf("We are calling the C function form C++ file.\n");

    add_func(10,15);

    return 0;
}
@H_41_2@makefile是:

CC = gcc
inc = -I include

vpath %.c src
vpath %.cpp src
vpath %.o obj

all : $(addprefix obj/,functions.o main.o) run

run : main.o functions.o
    $(CC) -o bin/$@ $^

obj/%.o : %.cpp
    $(CXX) -c $(inC) $^ -o $@ -libstdc++

obj/%.o : %.c
    $(CC) -c $(inC) $^ -o $@

.PHONY : clean

clean :
    -rm -f obj/* bin/*

我收到以下错误

g++ -c -I include src/functions.cpp -o obj/functions.o
gcc -c -I include src/main.c -o obj/main.o
gcc -o bin/run obj/main.o obj/functions.o
obj/functions.o: In function `add_func':
functions.cpp:(.text+0x1E): undefined reference to `std::cout'
functions.cpp:(.text+0x23): undefined reference to `std::basic_ostream<char,std::char_Traits<char> >& std::operator<< <std::char_Traits<char> >(std::basic_ostream<char,std::char_Traits<char> >&,char const*)'
functions.cpp:(.text+0x2d): undefined reference to `std::ostream::operator<<(int)'
functions.cpp:(.text+0x32): undefined reference to `std::basic_ostream<char,std::char_Traits<char> >& std::endl<char,std::char_Traits<char> >(std::basic_ostream<char,std::char_Traits<char> >&)'
functions.cpp:(.text+0x3a): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
obj/functions.o: In function `__static_initialization_and_destruction_0(int,int)':
functions.cpp:(.text+0x68): undefined reference to `std::ios_base::Init::Init()'
functions.cpp:(.text+0x77): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status
make: *** [run] Error 1

>如果我使用g作为链接器,那么它工作正常.但gcc链接器给了我
问题.
>我想到的问题是@L_696_11@名称由g和
gcc不会破坏@L_696_11@名称(符号).
>那么是否有任何编译器标志可以避免名称损坏,如果可能的话.
>避免g链接器的原因是它将链接视为C样式,但基本代码在C中,因此它可能会在调用代码时引入一些错误.

有人建议解决方案.

解决方法

使用

run : main.o functions.o
    $(CXX) -o bin/$@ $^

而是使用g链接器将所有内容链接在一起,并自动设置链接的libstc .a库的认值.

否则,您需要明确指定-lstc作为附加库.

此外,如果你正在为你的c代码设计一个纯c-API,你应该有

#include <iostream>

仅在functions.cpp源文件中的语句.从functions.h中@L_262_33@c标准头文件.

请不要使用Namespace std;一般在c头文件中.它很容易调用所有意外类型的命名空间冲突(没有人知道命名空间std中使用的所有名称).

它无论如何都不应出现在纯c-API头文件中.

如果您想要一个混合的c / c API头文件,请将所有c特定语句放在c guards中:

#ifdef __cplusplus
 #include<iostream>

 class xyz; // A c++ for@R_616_10225@ declaration
 #endif

大佬总结

以上是大佬教程为你收集整理的使用c中用c编写的函数全部内容,希望文章能够帮你解决使用c中用c编写的函数所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。