我是C++ noob,現在擺弄了幾個小時的下列問題。希望有人能夠啓發我。如何將字符串類型參數傳遞給C++方法
我有內容,像這樣一個CPP文件:
TEST.CPP文件內容
#include <iostream>
#include <exception>
#include <stdlib.h>
#include <string.h>
using std::cin; using std::endl;
using std::string;
string foobar(string bar) {
return "foo" + bar;
}
int main(int argc, char* argv[])
{
string bar = "bar";
System::convCout << "Foobar: " << foobar(bar) << endl;
}
這一個編譯和運行良好。現在,我想放在foobar的到外部庫:
mylib.h文件內容
string foobar(string bar);
mylib.cpp文件內容
#include <string.h>
using std::cin; using std::endl;
using std::string;
string foobar(string bar) {
return "foo" + bar;
}
TEST.CPP文件內容
#include <iostream>
#include <exception>
#include <stdlib.h>
#include "mylib.h"
int main(int argc, char* argv[])
{
string bar = "bar";
System::convCout << "Foobar: " << foobar(bar) << endl;
}
我調整了我的Makefile,以便test.cpp編譯並鏈接mylib,但我總是遇到錯誤:
test.cpp::8 undefined reference to `foobar(std::string)
我該如何處理字符串參數?我的嘗試似乎在這裏完全錯誤。
問候 菲利克斯
顯示完整的鏈接聲明。 – trojanfoe
你是如何與外部圖書館聯繫的?你忘了添加'-lmylib'來鏈接參數嗎? –
你的意思是包括''而不是''? –