im having some trouble to understand why im getting an compiling error. Also for some reason my target_link_libraries() is not recognized (look main.cpp). Here my compiling error:
====================[ Build | phantom_omni_BA | Debug ]=========================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/alexanderalbrecht/CLionProjects/phatom_omni_BA/cmake-build-debug --target phantom_omni_BA -j 3
[2/2] Linking CXX executable src/app/phantom_omni_BA
FAILED: src/app/phantom_omni_BA
: && /Library/Developer/CommandLineTools/usr/bin/c -g -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names src/app/CMakeFiles/phantom_omni_BA.dir/main.cpp.o -o src/app/phantom_omni_BA && :
Undefined symbols for architecture x86_64:
"generateCode(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
_main in main.cpp.o
"loadSettings(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
_main in main.cpp.o
"saveSettings(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::vector<int, std::__1::allocator<int> >)", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
Here are my files
CMakeLists.txt
cmake_minimum_required(VERSION 3.22)
project(phatom_omni_BA)
set(CMAKE_CXX_STANDARD 14)
if(MSVC)
add_compile_options(/W4)
else()
add_compile_options(-Wall -Wextra -pedantic)
endif()
add_subdirectory(src)
src(directory)
CMakeLists.txt
add_subdirectory(app)
add_subdirectory(libSettings)
app(directory)
main.cpp
#include <iostream>
#include <vector>
#include "../libSettings/saveSettings.h"
#include "../libSettings/loadSettings.h"
#include "../libSettings/generateSubjectCode.h"
using std::vector;
int main() {
vector<int> bspArray = {1, 2, 3, 4, 5};
saveSettings("Experiment9-Proband2-09:11", bspArray);
loadSettings("Experiment7-Proband2-09:11");
cout << generateCode("Experiment1");
return 0;
}
CMakeLists.txt
add_executable(phantom_omni_BA main.cpp)
libSettings(directory)
CMakeLists.txt
add_library(Settings datetime.cpp generateSubjectCode.cpp loadSettings.cpp saveSettings.cpp)
target_link_libraries(Settings PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
datetime.h
#ifndef PHATOM_OMNI_BA_DATETIME_H
#define PHATOM_OMNI_BA_DATETIME_H
#include <sstream>
using std::string;
const string currentDateTime();
#endif //PHATOM_OMNI_BA_DATETIME_H
datetime.cpp
#include "datetime.h"
/**
* Get current date/time, format is YYYY-MM-DD.HH:mm:ss
* @return
*/
const string currentDateTime() {
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
strftime(buf, sizeof(buf), "%Y-%m-%d-%X", &tstruct);
return buf;
}
generateSubjectCode.h
#ifndef PHATOM_OMNI_BA_GENERATESUBJECTCODE_H
#define PHATOM_OMNI_BA_GENERATESUBJECTCODE_H
#include <sstream>
using std::string;
int subjectNumber = 0;
string generateCode(string experimentName);
#endif //PHATOM_OMNI_BA_GENERATESUBJECTCODE_H
generateSubjectCode.cpp
#include "generateSubjectCode.h"
#include "datetime.h"
/**
* Generates code snippet
* @param experimentName
* @return code this is the code snippet
*/
string generateCode(std::string experimentName) {
string code = " " experimentName "-" "Proband" to_string(subjectNumber) "-" currentDateTime();
subjectNumber ;
return code;
}
loadSettings.h
#ifndef PHATOM_OMNI_BA_LOADSETTINGS_H
#define PHATOM_OMNI_BA_LOADSETTINGS_H
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
using std::string;
using std::cout;
using std::vector;
using std::stringstream;
using std::fstream;
using std::ios;
vector<vector<string>> loadSettings(string fname);
#endif //PHATOM_OMNI_BA_LOADSETTINGS_H
loadSettings.cpp
#include "loadSettings.h"
/**
* Searches through directory
* If settings have been already created, then load those files
* @param fname
* @return
*/
vector<vector<string>> loadSettings(string fname) {
vector<vector<string>> content;
vector<string> row;
string line, word;
fstream file (fname, ios::in);
if(file.is_open())
{
// Get line of csv file
while(getline(file, line))
{
row.clear();
stringstream str(line);
// Get element for element, separated through ","
while(getline(str, word, ',')) {
row.push_back(word);
}
content.push_back(row);
}
}
else {
cout<<"Could not open the file\n";
}
return content;
for(int i=0;i<content.size();i )
{
for(int j=0;j<content[i].size();j )
{
cout<<content[i][j]<<" ";
}
cout<<"\n";
}
}
saveSettings.h
#ifndef PHATOM_OMNI_BA_SAVESETTINGS_H
#define PHATOM_OMNI_BA_SAVESETTINGS_H
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
using std::string;
using std::vector;
using std::ofstream;
using std::to_string;
void saveSettings(string subjectCode, vector<int> settingsArray);
#endif //PHATOM_OMNI_BA_SAVESETTINGS_H
saveSettings.cpp
#include "saveSettings.h"
/**
* Saves Settings of user
* As CSV File
* @param subjectCode
* @param settingsArray
*/
void saveSettings(string subjectCode, vector<int> settingsArray) {
// Create output filestream object
ofstream myFile(subjectCode);
// Fill Object with data
for (int i = 0; i < settingsArray.size(); i ) {
myFile << to_string(settingsArray[i]) ",";
}
// Close file
myFile.close();
}
Here also my configurations: Run Configurations Toolchain
CodePudding user response:
You link the lib with itself.
libSettings/CMakeLists.txt:
add_library(Settings datetime.cpp generateSubjectCode.cpp loadSettings.cpp saveSettings.cpp)
target_link_libraries(Settings PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
Move the last line into CMakeLists.txt
add_executable(phantom_omni_BA main.cpp)
target_link_libraries(phantom_omni_BA PRIVATE Settings)