小编Abh*_*ngh的帖子

在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
Run Code Online (Sandbox Code Playgroud)

cpp文件是:

#include"h_files.h"

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

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;
}
Run Code Online (Sandbox Code Playgroud)

makefile是:

CC = gcc
inc = -I include

vpath %.c src
vpath %.cpp src
vpath %.o …
Run Code Online (Sandbox Code Playgroud)

c c++ linux gcc

6
推荐指数
2
解决办法
358
查看次数

Makefile DEFAULT_GOAL 变量

这是使用 DEFAULT_GOAL 变量的示例:

ifeq ($(.DEFAULT_GOAL),)
  $(warning no default goal is set)
endif

.PHONY: foo
foo: ; @echo $@

$(warning default goal is $(.DEFAULT_GOAL))

# Reset the default goal.
.DEFAULT_GOAL :=

.PHONY: bar
bar: ; @echo $@

$(warning default goal is $(.DEFAULT_GOAL))

# Set our own.
.DEFAULT_GOAL := foo


The output is:

no default goal is set
default goal is foo
default goal is bar
foo
Run Code Online (Sandbox Code Playgroud)

我坚持理解echo 和 $(warning )函数的流程是什么,即当调用$(warning )函数时, echo $@输出被抑制,并显示echo $@的最后一个输出。因为有 …

linux makefile

5
推荐指数
1
解决办法
5138
查看次数

来自C中存档(.a)文件的共享库

我被赋予了从对象文件创建存档文件.a的任务,并从存档.a文件创建共享库文件.我尝试过以下文件:
foo.h

#ifndef _foo_h__
#define _foo_h__
extern void foo(void);
extern void bar(void); 
#endif //_foo_h__
Run Code Online (Sandbox Code Playgroud)


foo.c的

#include<stdio.h>

void foo(void)
{
    puts("Hello, I'm a shared library");
}
Run Code Online (Sandbox Code Playgroud)


bar.c

#include<stdio.h>

void bar(void)
{
    puts("This is bar function call.");
}
Run Code Online (Sandbox Code Playgroud)


main.c中

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

int main(void)
{
    puts("This is a shared library test...");
    foo();
    bar();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)


Makefile文件

CFLAGS = -Wall -Werror
LDFLAGS = -L/home/betatest/Public/implicit-rule-archive -Wl,-rpath,'$$ORIGIN'

all : run

run : main.o libfoo.so
    $(CC) $(LDFLAGS) -o $@ $^

libfoo.so : CFLAGS …
Run Code Online (Sandbox Code Playgroud)

c linux makefile shared-libraries

3
推荐指数
1
解决办法
1697
查看次数

printf结果是意外的

我有以下代码:

char *buff = (char *)malloc(sizeof(char)*120);
char *ts_t = (char *)malloc(sizeof(char)*80);

/*Generating invalid test case for timestamp.*/
printf("Out side of ts.\n");

 while(fgets (ts_t, 80, fp_ts)!=NULL) 
 {
      printf("Inside ts.\n");
      //ts_t[strlen(ts_t)-1] = '\0';
      memset(buff,0,120);       
      memcpy(buff,ts_t,strlen(ts_t)-1);

      printf("The ts is :%s",buff);

      fprintf(fp_test_case,"%s\t%s\t%s%d\t%s\t%s\t%s%04d\n",buff,ver,txn,digit_generate(num),aspid,uid,"Test",count_testCase);

      print_description( fp_description,ts_t,count_testCase,"TimeStamp");
      count_testCase++;
    }

printf("Invalid Time stamp case generated.\n");
Run Code Online (Sandbox Code Playgroud)

预期的是:

在外面.
里面的ts.
ts是:05-26-2015T13:53:33.509
内部ts.
ts是:05-26-2015T13:53:33.509

但它打印为:

在ts
里面.
里面的ts.
Inside ts.:05-26-2015T13:53:33.509
Inside ts.:2015-26-05T13:53:33.509

我在这做错了什么?谢谢.

c linux gcc

2
推荐指数
1
解决办法
63
查看次数

While while和While循环意外退出?

我正在使用gcc-4.7在Centos 6.5上编写以下C代码,但是程序不等待类型Y/N:语句在结果后立即退出.其中任何一个代码都不起作用.

#include <stdio.h>

int main()
{
    int a,b;
    char ch='y';

   do
    {
            printf("Enter Number 1:\n");
            scanf("%d",&a);
            printf("Enter Number 2:\n");
            scanf("%d",&b);
            printf("Result is:%d\n",a+b);
            printf("Type y/N:\n");
            scanf("%c",&ch);
    }while(ch=='y' || ch=='Y');
    }
    return 0;
 }
Run Code Online (Sandbox Code Playgroud)

要么

    #include<stdio.h>

   int main()
    {
      int a,b;
      char ch='y';

       while(ch=='y' || ch=='Y')
       {
            printf("Enter Number 1:\n");
            scanf("%d",&a);
            printf("Enter Number 2:\n");
            scanf("%d",&b);
            printf("Result is:%d\n",a+b);
            printf("Type y/N:\n");
            scanf("%c",&ch);

       }
         return 0;
}
Run Code Online (Sandbox Code Playgroud)

c centos do-while

0
推荐指数
1
解决办法
191
查看次数

标签 统计

c ×4

linux ×4

gcc ×2

makefile ×2

c++ ×1

centos ×1

do-while ×1

shared-libraries ×1