简体   繁体   English

从其他 C++ 源调用 function 不起作用

[英]Calling function from other C++ source doesn't work

I've looked at several posts of the same questions on here and as far as I've figured I've done what they said to do.我在这里查看了几个相同问题的帖子,据我所知,我已经按照他们所说的做了。 However, I still get a "undefined reference to `cmb::functionA()'" warning.但是,我仍然收到“对 `cmb::functionA()' 的未定义引用”警告。

I have the header:我有 header:

//combine.h
#ifndef COMBINE_H
#define COMBINE_H

namespace cmb 
{
    void functionA();
}

#endif

Function source file: Function源文件:

// combine.cc
#include <iostream>
#include "combine.h"
using namespace std;

namespace cmb
{
    void functionA()
    {
        cout << "print something\n"; 
    }
}

And main:主要:

//main.cc
#include "combine.h"
using namespace std;
using namespace cmd;

int main(int argc, char *argv[])            
{   
    functionA();
}

It is now working when compiling manually (g++ -o Test *.cc -Wall --std=c++17) but using make still gives me the same error.它现在在手动编译时工作(g++ -o Test *.cc -Wall --std=c++17),但使用 make 仍然给我同样的错误。 I really don't understand make files so any help would be appreciated.我真的不明白制作文件,所以任何帮助将不胜感激。

makefile: makefile:

CXX := g++
CXXFLAGS += -Wall -std=c++17

LIBSRCS = $(filter-out main.cc,$(shell find -name \*.cc))
LIBOBJS = $(patsubst %.cc,%.o,$(LIBSRCS))

main: main.o combine.o libproject.a
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) -o $@ $<

$(LIBOBJS): %.o: %.cc
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<

libproject.a: $(LIBOBJS)
    ar rcs $@ $^
clean:
    rm -f libproject.a $(LIBOBJS)
.PHONY: clean

I just use make main in terminal.我只是在终端中使用make main

You must add combine.o after main: in the makefile.您必须在 makefile 中的 main: 之后添加 combine.o。

Since you use a library, you need to tell the linker to use it (LDFLAGS), and it should be after the main in g++ command.由于您使用库,因此您需要告诉 linker 使用它(LDFLAGS),并且它应该在 g++ 命令中的 main 之后。 As in previous comments, the using namespace cmd needed to be changed to cmb与之前的评论一样,需要将 using namespace cmd 更改为 cmb

This one worked for me:这个对我有用:

CXX := g++
CXXFLAGS += -Wall -std=c++17

LIBSRCS = $(filter-out ./main.cc,$(shell find -name \*.cc))
LIBOBJS = $(patsubst %.cc,%.o,$(LIBSRCS))
LDFLAGS += -L. -lproject

main: main.o libproject.a
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ $< $(LDFLAGS) 

libproject.a: $(LIBOBJS)
    ar rcs $@ $^

.PHONY: clean
clean:
    rm -f libproject.a $(LIBOBJS) main main.o

I also needed to add./ in filtering out main.cc我还需要在过滤掉 main.cc 时添加 ./

Example run:示例运行:

jontte@jontte-Latitude-E5420:~/Temp/maketest$ make
g++ -Wall -std=c++17   -c -o main.o main.cc
g++ -Wall -std=c++17   -c -o combine.o combine.cc
ar rcs libproject.a combine.o
g++  -Wall -std=c++17 -o main main.o -L. -lproject 
jontte@jontte-Latitude-E5420:~/Temp/maketest$ ./main 
print something
jontte@jontte-Latitude-E5420:~/Temp/maketest$ make clean
rm -f libproject.a ./combine.o main main.o
jontte@jontte-Latitude-E5420:~/Temp/maketest$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM