Catch2 C++ linker problem -- undefined symbols for classes included - c++

Working on a c++ school project. Can't seem to get catch2 testing to work correctly.
I have a makefile where I compile my project
CC=clang++
CFLAGS=--std=c++11
objects = Event.o Simulation.o ListItem.o Node.o OrderedItem.o PartArrival.o
PriorityQueue.o Queue.o PartOne.o PartTwo.o PartZero.o PartialProduct.o
ProductArrival.o StartAssembly.o StartFinishingAssembly.o EndFinishingAssembly.o
StartMainAssembly.o EndMainAssembly.o EndAssembly.o Test_PriorityQueue.o
# .. etc .. put a list of your .o files here
# this rule will build A2 as the executable from the object files
all : A2main.o $(objects)
$(CC) $(CFLAGS) -o A2 $< $(objects)
# this rule will build A2test -- our testfile for the PriorityQueue
all : Test_PriorityQueue.cpp
clang++ --std=c++11 -o A2test Test_PriorityQueue.cpp
# this rule will build a .o file from a .cpp file.
%.o: %.cpp
$(CC) -c -o $# $< $(CFLAGS)
When this line runs clang++ --std=c++11 -o A2test Test_PriorityQueue.cpp I get this error:
Undefined symbols for architecture x86_64:
"Simulation::Simulation()", referenced from:
____C_A_T_C_H____T_E_S_T____4() in Test_PriorityQueue-1f17a8.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [all] Error 1
Here's the code for the test file -- Test_PriorityQueue.cpp :
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do
this in one cpp file
#include "catch.hpp"
#include "Simulation.h"
#include "PartArrival.h"
#include "PriorityQueue.h"
#include <iostream>
using namespace std;
unsigned int Factorial( unsigned int number ) {
return number <= 1 ? number : Factorial(number-1)*number;
}
TEST_CASE( "Factorials are computed", "[factorial]" ) {
REQUIRE( Factorial(1) == 1 );
REQUIRE( Factorial(2) == 2 );
REQUIRE( Factorial(3) == 6 );
REQUIRE( Factorial(10) == 3628800 );
}
// debugging test
TEST_CASE("CATCH TEST"){
REQUIRE(1 == 1);
}
TEST_CASE("create PQ") {
cout << "PQ testcase" << endl;
Simulation *sim = new Simulation();
// PriorityQueue *pQue = new PriorityQueue();
// REQUIRE(pQue->getSize() == 0);
}
The first few tests run just fine when I comment out the last one "create PQ"
Once I include that I start getting these undefined symbol errors.
Been bashing my head against this for awhile. Some help would be very appreciated!!
Edit:
Simulation.h
#ifndef START_FILES_SIMULATION_H
#define START_FILES_SIMULATION_H
#pragma once
#include <fstream>
using namespace std;
class PriorityQueue; // Priority Queue
class Queue; // Queue class - provided to you
class Event; // Event - given to you.
class PartZero; // p0
class PartOne; // p1
class PartTwo; // p2
class PartialProduct; // partially assembled product
class Simulation {
private:
ifstream ifile; // input file to read.
int simulationTime; // what is the current time of the simulation?
PriorityQueue *eventList; // priority queue of Events.
Queue* productQueue; // queue of partially assembled products (for finishing station).
Queue** partQueues; // *array* of queues of parts for the stations.
int mainAssemblyTime; // how long does the main station take?
int finishingAssemblyTime; // how long does the main station take?
bool mainBusy; // is the main station busy?
bool finishingBusy; // is the finishing station busy?
int completelyAssembledItems; // number of items made
int cumulativeBuildTime; // the total amount of time to build all items
float averageBuildTime; // avg time to build item
public:
Simulation(); //TODO: build the part Queues when simulation is built
// TODO: you need methods to manipulate product and part queues.
// [add them here.]
// add to the respective queues
void addPartZero(PartZero *part);
void addPartOne(PartOne *part);
void addPartTwo(PartTwo *part);
void addPartialProduct(PartialProduct * product);
// dequeue the respective queues
int popMainParts(); // pops p0 and p1 and returns the earliest of arrivalTimes
void popPartZero();
void popPartOne();
void popPartTwo();
PartialProduct* popPartialProduct();
// functions that check if queues have parts in
bool partsInZero();
bool partsInOne();
bool partsInTwo();
bool partsInProduct();
int getCompletelyAssembledItems(); // getter
void completeAssemblyOfItem(); // adds one to completelyAssembledItems var
void updateCumulativeBuildTime(int time);
float getAvgBuildTime();
// main method for driving the simulation
void runSimulation(char *fileName);
// add an event to event queue.
void addEvent (Event*);
// read next arrival from file and add it to the event queue.
bool getNextArrival(); //TODO: add to sim.cpp
// getter and setter for simulation time
int getSimulationTime();
void setSimulationTime(int time);
// getters for assembly times
int getMainTime();
int getFinishingTime();
// getters and setters for station statuses.
bool isMainBusy();
bool isFinishingBusy();
void setMainStatus(bool);
void setFinishingStatus(bool);
};// class Simulation
#endif //START_FILES_SIMULATION_H
Simulation.cpp constructor :
// constructor
Simulation::Simulation() {
// init part queues
partQueues = reinterpret_cast<Queue **>(new Queue[3]);
partQueues[0] = new Queue();
partQueues[1] = new Queue();
partQueues[2] = new Queue();
// init event list
eventList = new PriorityQueue();
// init product queue
productQueue = new Queue();
// station status
mainBusy = false;
finishingBusy = false;
}

Related

C++ in ARM MCU: Need help to set up a simple timer

I'm programming an ATSAME70 and I'm trying to program a simple timer using the SysTick interrupt available in Cortex M MCUs, but I don't know what is going wrong.
If write this code in a simple main.cpp file:
// main.cpp
#include <cstdint>
#include "init.h"
#include "led.hpp"
volatile uint32_t g_ticks = 0;
extern "C" {
void SysTick_Handler(void)
{
g_ticks++;
}
}
class Timer
{
private:
uint32_t start;
public:
Timer() : start(g_ticks) {}
float elapsed() const { return (g_ticks - start) / 1000.0f; }
};
int main()
{
init();
SysTick_Config(300000000 / 1000); /* Clock is running at 300 MHz */
Timer t;
while (t.elapsed() < 1.0f);
Led::on();
while (true);
}
It works, the led lights up properly after 1 second.
But if I try to keep it clean and separate the program in the following files:
// timer.hpp
#include <cstdint>
class Timer
{
private:
uint32_t start;
public:
Timer();
float elapsed() const;
};
// timer.cpp
#include "timer.hpp"
volatile uint32_t g_ticks = 0;
extern "C" {
void SysTick_Handler(void)
{
g_ticks++;
}
}
Timer::Timer() : start(g_ticks) {}
float Timer::elapsed() const
{
return (g_ticks - start) / 1000.0f;
}
// main.cpp
#include <cstdint>
#include "init.h"
#include "led.hpp"
#include "timer.hpp"
int main()
{
init();
SysTick_Config(300000000 / 1000); /* Clock is running at 300 MHz */
Timer t;
while (t.elapsed() < 1.0f);
Led::on();
while (true);
}
It doesn't work anymore, the program reaches the first while loop and then it gets stuck there, I think g_ticks is being corrupted when I try to read it in t.elapsed() but I don't know what is happening. Does anybody know where I'm wrong?
init() is just a function in which I initialize all needed registers.
EDIT: here are the command lines used to generate the code:
$toolchain_path = "C:\Program Files (x86)\GNU Tools ARM Embedded\8 2018-q4-major\bin";
$link_file = "source\device\same70_flash.ld"
$c_files = "include\sensors\bmi088\bmi088.c " +
...
"source\utils\syscalls.c";
$cpp_files = "source\device\init.cpp " +
...
"source\main.cpp";
Invoke-Expression "& '$toolchain_path\arm-none-eabi-gcc.exe' -c -s -O3 -fdata-sections -ffunction-sections '-Wl,--gc-sections' '-Wl,--entry=Reset_Handler' -mthumb -mcpu=cortex-m7 -mfloat-abi=hard -mfpu=fpv5-d16 -Isource -Iinclude\CMSIS -D__SAME70N21__ $c_files --specs=nosys.specs"
foreach ($c_file in $c_files.split(" "))
{
if ($objects) { $objects += " "; }
$objects += ($c_file.split("\")[-1]).split(".")[0] + ".o";
}
Invoke-Expression "& '$toolchain_path\arm-none-eabi-ld.exe' -s --entry=Reset_Handler -r $objects -o drivers.o"
foreach ($object in $objects.split(" ")) { Remove-Item $object; }
Move-Item drivers.o bin\drivers.o -force
Invoke-Expression "& '$toolchain_path\arm-none-eabi-g++.exe' -s -O3 -fdata-sections -ffunction-sections '-Wl,--gc-sections' -mthumb -mcpu=cortex-m7 -mfloat-abi=hard -mfpu=fpv5-d16 '-Wl,--entry=Reset_Handler' -std=c++17 -Isource -Iinclude -Iinclude\CMSIS -D__SAME70N21__ bin/drivers.o $cpp_files --specs=nosys.specs -T $link_file -o bin\code.elf"
Invoke-Expression "& '$toolchain_path\arm-none-eabi-objcopy.exe' -O binary bin\code.elf bin\code.bin"
The script is written in powershell and I'll explain it a little bit. $c_files is just a string with every c file to be compiled separated by an space. $objects is an array of strings containing every file listed in $c_files but with the ".c" extension replaced by ".o". I've done this to link every c compiled file into "drivers.o". Finally, c++ code is compiled using this drivers.o as argument and then I generate the .bin file to upload it to the MCU.
The code is compiled using the latest GNU Arm Embedded toolchain. I must have made a mistake somewhere but I don't know where and I don't have a debugger to debug the code at runtime.
EDIT 2: Both variants work properly without optimizations. If I pass -O1 or higher as argument to the compiler the second variant stops working and I don't understand why.

C++ undefined reference to

I get a lot of undefined references. I don't know what I'm doing wrong.
I'm getting the following errors:
undefined reference to 'LetteroidField::start()'
undefined reference to 'LetteroidField::setTitle(std::string)'
undefined reference to 'Letteroid::setletter(char)'
undefined reference to 'Letteroid::setLetter()'
undefined reference to 'Letteroid::setCoords()'
undefined reference to 'Letteroid::erase()'
and other letteroid references.
I'm not done with the other classes, but I don't know why I'm getting these errors. Am I not using #include "" correctly?
This is my professor's sample code. I contacted him but he is not answering (its an online class).
#include "letteroidfield.h"
#include "letteroid.h"
#include "blinkingletteroid.h"
#include "jumpingletteroid.h"
#include "movingletteroid.h"
#include <stdlib.h> /* srand, rand */
#include <time.h>
/// include your derived classes here
int main()
{
LetteroidField screen;
screen.start();
screen.setTitle("Ken's example for the class, press 'x' to quit");
BlinkingLetteroid one;
BlinkingLetteroid two;
BlinkingLetteroid three;
one.setLetter('!'); /// character
one.setCoords(5, 10); /// row, col
two.setLetter('h');
two.setCoords(7, 9);
three.setLetter('#');
three.setCoords(15, 57);
JumpingLetteroid four;
four.setLetter('j');
four.setCoords(rand() % 21, rand() % 21);
MovingLetteroid five;
five.setLetter('m');
int x = 20;
int y = 20;
while (x >= 1)
{
--x;
}
while (y >= 1)
{
--y;
}
if (x == 1)
{
x = 20;
}
if (y == 1)
{
x = 20;
}
five.setCoords(x,y);
/// create and initialize your letteroids here
while ( screen.waitForKeyPress() ) /// keep going until 'x' is pressed
{
one.blink();
two.blink();
three.blink();
/// call the function that draws your letteroids here
}
screen.end();
return 0;
}
#ifndef _LETTEROIDFIELD_H_
#define _LETTEROIDFIELD_H_
#include <string>
class LetteroidField
{
public:
void start(); /// start up the screen for letteroids
bool waitForKeyPress(); /// wait for any key to be pressed (return
void end(); /// shut down the screen and return it to
void setTitle(std::string); /// diplay the title
};
#endif
#ifndef _LETTEROID_H_
#define _LETTEROID_H_
class Letteroid
{
public:
void setCoords(int, int);// set the position(down, across)
void setLetter(char); // set the character
int getX(); // get the position down
int getY(); // get the position across
void erase(); // erase the letteroid from the screen
void draw(); // draw the letteroid to the screen
private:
int myX;
int myY;
char myLetter;
};
#endif
The question you need to ask yourself is: Where are those classes defined?
If the answer is: "in a shared library (file extension ".so") provided alongside the header", then you'll need to link against it by adding at least the following to your compilation command:
g++ main.cpp -L</path/to/library> -l<library_name>
If the answer is: "in a static library (file extension ".a", AKA archive) provided alongside the header", then you'll need include it in your binary by adding at least the following to your compilation command:
g++ main.cpp <library_name.a>
If the answer is: "in a bunch of source files provided alongside the header", then you'll need to include them in your binary by adding at least the following to your compilation command:
g++ main.cpp <source_file1.cpp> <source_file2.cpp> ...

c++ Compile error - Undefined symbols for architecture x86_64 [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Undefined symbols for architecture i386:
using namespace std;
#include <iostream>
#include <fstream>
#include <string>
#include "CorpusExp.h"
int main(int argc, char* argv[]){
ifstream infile("../DATA.txt");
string train_dir; // The training data directory
infile>>train_dir;
train_dir=train_dir.substr(6,train_dir.length());
if (argc>1){
if (strcmp(argv[1],"-s")){ // enter into CorpusExploration mode
CorpusExp ce(train_dir,"all"); //<<=======LINE X!!!!!!!!
//ce.calculate();
if (argc>=3 && strcmp(argv[2],"-u")){ // check user stats
cout<<"shit";
}
else if (argc>=3 && strcmp(argv[2],"-m")){ // check movie stats
}else{ // check the all (default) stats
}
}else if(strcmp(argv[1],"-m")) {// enter into Recommendation mode
}
}
return 0;
}
If I include the LINE X, the main.cpp won't compile, the error message is:
Undefined symbols for architecture x86_64:
"CorpusExp::CorpusExp(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [recommend] Error 1
And here is my CorpusExp.cpp and CorpusExp.h:
/* CorpusExporater header file
Used for statistical purpose
*/
using namespace std;
#include <iostream>
#include <fstream>
#include <string>
#include <dirent.h> // the Linux library to read the directories
class CorpusExp{
public:
CorpusExp(const string&,const string&); // (dir_name, mode)
void calculate();
void display(ostream &out, const int&); // print out the id's stat information
private:
string _dir_name;
int _movie_count;
int _user_count;
int _movie_rated_count[5]; // times of movie rated as 1,2,3,4,5
float _movie_avg; // total moview average rates
string _mode; // the mode of current task
int _id; // the id of current task
};
/*CorpusExporater cpp file
Used for statistical purpose
*/
#include "CorpusExp.h"
CorpusExp::CorpusExp(const string &dir_name, const string &mode):
_dir_name(dir_name),
_mode(mode),
_movie_count(0),
_user_count(0)
{
}
void CorpusExp::calculate(){
DIR *dpdf;
struct dirent *epdf;
dpdf = opendir(strdup(_dir_name.c_str()));
if (dpdf!=NULL){
while(epdf = readdir(dpdf)){
cout<<epdf->d_name<<endl;
}
}
}
void CorpusExp::display(ostream &out, const int &id){
}
I am barely new to c++, so I get very confused, don't know which part caused this problem. If I remove the LINE X, everything will just be fine, but if I declare the CorpusExp class in the main function, then it did not compile......
here is the compile file:
# Makefile for Movie Recommendation--- HW5 of Search Engine 11-641
# Yitong Zhou
# Nov 19, 2012
#source code directory:
src=src
#obj code directory:
obj=obj
recommend: $(obj)/main.o $(obj)/CorpusExp.o
g++ -o $# $<
$(obj)/main.o: src/main.cpp src/CorpusExp.cpp
g++ -c $< -o $#
$(obj)/CorpusExp.o: $(src)/CorpusExp.cpp $(src)/CorpusExp.h
g++ -c $< -o $#
clean:
rm -rf obj/*.o recommend
Try a simple command line compile without the makefile to make sure the makefile itself isn't the problem.
cd src; g++ main.cpp CorpusExp.cpp -o recommend

C++ Multiple Definitions

I'm trying to build these files but it's giving me a multiple definition error.
main.cpp:
#include "SDL/SDL.h"
#include "Core.h"
#include "GameStates.h"
#include "globals.h"
int main(int argc, char** args)
{
if(core.Initilization(640, 480, 32, SDL_SWSURFACE) == -1)
{
SDL_Quit();
}
while(core.desiredstate != core.quit)
{
::currentstate->EventHandling();
::currentstate->Logic();
core.ChangeState();
::currentstate->Render();
::currentstate->Update();
}
SDL_FreeSurface(core.screen);
SDL_Quit();
}
Core.cpp:
#include "Core.h"
#include "GameStates.h"
#include "SDL/SDL.h"
#include "Intro.h"
#include "globals.h"
#include <string>
/* Starts SDL subsystems and sets screen attributes */
bool Core::Initilization(int SCREEN_WIDTH, int SCREEN_HEIGHT, int SCREEN_BPP, int FLAGS)
{
//starts SDL subsystems, returns false upon error
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
return false;
}
//The screen
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, FLAGS);
//Returns false if there was an error
if(screen == NULL)
{
return false;
}
SDL_WM_SetCaption("Game", NULL);
return true;
}
/* Loads an image and optimizes it */
SDL_Surface* Core::Load(std::string filename)
{
//original loaded image
SDL_Surface* original = SDL_LoadBMP(filename.c_str());
SDL_Surface* optimized = NULL;
if(original != NULL)
{
//Sets optimized to optimized version of original
optimized = SDL_DisplayFormat(original);
SDL_FreeSurface(original);
}
return optimized;
}
/* Blits surfaces */
void Core::ApplySurface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
//holds the x y coordinates
SDL_Rect location;
location.x = x;
location.y = y;
if(destination != NULL)
{
SDL_BlitSurface(source, NULL, destination, &location);
}
}
/* Sets desiredstate to be used in ChangeState(); */
void Core::SetState(int newstate)
{
if(desiredstate != state_null && desiredstate != quit)
{
desiredstate = newstate;
}
}
/* Changes the game state */
void Core::ChangeState()
{
if(desiredstate != state_null && desiredstate != quit)
{
//frees old state memory
delete ::currentstate;
switch(desiredstate)
{
case intro:
//allocates new state memory
::currentstate = new Intro();
break;
}
stateID = desiredstate;
desiredstate = state_null;
}
}
GameStates.h:
#ifndef GAMESTATES_H
#define GAMESTATES_H
class GameStates
{
public:
virtual void EventHandling() = 0;
virtual void Logic() = 0;
virtual void Render() = 0;
virtual void Update() = 0;
};
#endif
Intro.h:
#ifndef INTRO_H
#define INTRO_H
#include "SDL/SDL.h"
#include "GameStates.h"
class Intro : public GameStates
{
private:
SDL_Surface* test;
public:
Intro();
void EventHandling();
void Logic();
void Render();
void Update();
~Intro();
} intro;
#endif
Intro.cpp:
#include "Intro.h"
#include "GameStates.h"
#include "Core.h"
#include "SDL/SDL.h"
Intro::Intro()
{
test = core.Load("test.bmp");
}
void Intro::EventHandling()
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
core.SetState(core.quit);
break;
}
}
}
void Intro::Logic()
{
//to be coded when the program actually builds...
}
void Intro::Render()
{
core.ApplySurface(30, 30, test, core.screen);
}
void Intro::Update()
{
SDL_Flip(core.screen);
}
Intro::~Intro()
{
SDL_FreeSurface(test);
}
globals.h:
#include "GameStates.h"
#include "SDL/SDL.h"
GameStates* currentstate = NULL;
Sorry if the indentation is off; having to put four spaces for it to be seen as a code block messed with it a bit.
Heres the error message:
/tmp/ccWxKsO5.o:(.bss+0x0): multiple definition of `core'
/tmp/cc13Eqmt.o:(.bss+0x0): first defined here
/tmp/ccWxKsO5.o:(.bss+0x20): multiple definition of `currentstate'
/tmp/cc13Eqmt.o:(.bss+0x10): first defined here
/tmp/ccJXxewI.o:(.bss+0x0): multiple definition of `intro'
/tmp/ccWxKsO5.o:(.bss+0x10): first defined here
/tmp/ccJXxewI.o:(.bss+0x10): multiple definition of `core'
/tmp/cc13Eqmt.o:(.bss+0x0): first defined here
collect2: ld returned 1 exit status
Makefile:
OBJS = main.o Intro.o Core.o
CC = g++
DEBUG = -g
CFLAGS = -Wall -c $(DEBUG)
LIBS = -lSDL
game : $(OBJS)
$(CC) $(CFLAGS) $(OBJS) -o game $(LIBS)
main.o : Core.h GameStates.h globals.h
$(CC) $(CFLAGS) main.cpp $(LIBS)
Core.o : Core.h Core.cpp GameStates.h Intro.h globals.h
$(CC) $(CFLAGS) Core.cpp $(LIBS)
Intro.o : Intro.cpp GameStates.h Core.h
$(CC) $(CFLAGS) Intro.cpp $(LIBS)
The problem isn't your code, it's your build system.
Any sane build system matches the name of the object files to the name of the source files. But you have ccWxKsO5.o and cc13Eqmt.o. What's worse, the build system appears to be trying to link multiple objects generated from the same source (perhaps some were created by an earlier run of the compiler).
tempnam and globbing *.o is not a reasonable way to build C++ programs.
Well, there may be some code problems also. But those will be a thousand times easier to find and fix once the object names in the error messages correlate to source files.
For objects shared between multiple translation units, the rule is: there must be exactly one definition, but you may have multiple declarations.
In practice, this means: put "extern Class object;" in your .h file, and "Class object;" in exactly one of your .CPP files.
For intro, for example, change your Intro.h to:
class Intro : public GameStates
{
... // whatever
};
extern Intro intro;
and add this line to Intro.cpp:
Intro intro;
Similarly for currentstate, in globals.h:
extern GameStates* currentstate;
and in one .CPP (it doesn't matter to the compiler which one):
GateStates* currentstate = NULL;
P.s. Your makefile is broken. You pass -c, which means "don't link" to your link step. Try this:
OBJS = main.o Intro.o Core.o
CC = g++
DEBUG = -g
CFLAGS = -Wall $(DEBUG)
LIBS = -lSDL
game : $(OBJS)
$(CC) $(CFLAGS) $(OBJS) -o game $(LIBS)
main.o : Core.h GameStates.h globals.h
$(CC) -c $(CFLAGS) main.cpp
Core.o : Core.h Core.cpp GameStates.h Intro.h globals.h
$(CC) -c $(CFLAGS) Core.cpp
Intro.o : Intro.cpp GameStates.h Core.h
$(CC) -c $(CFLAGS) Intro.cpp
In globals.h, you must declare currentstate extern. Then create globals.cpp, with the definition (GameStates* currentstate = NULL;). I couldn't find any reference for intro or core in your code, but it's probably the same problem: you can declare global variables as often as you want as long as you declare them extern, and only define them once per resulting binary, in only one translation unit.
Also, you probably want to add a header guard (#ifndef GLOBALS_H ...) to globals.h, just in case you add anything else in there.
put include guards in globals.h
make GameStates* declaration extern
//globals.h
#ifndef _MY_GLOBALS_H_
#define _MY_GLOBALS_H_
#include "GameStates.h"
#include "SDL/SDL.h"
extern GameStates* currentstate;
#endif
//Main.cpp
#include "globals.h"
GameStates* currentState = 0;

g++ shared library not found same directory with -L

I just cant see what Im doing wrong here:
Q. Why is my library not being found by g++ when compiling the program with a shared object?
Im trying to include a shared library to my c++ program:
g++ -fpic -c sha.cpp
g++ -shared -o libsha.so sha.o
g++ main.cpp -o main -L. -lsha
where sha.cpp and sha.h are the library files and main.cpp is my program.
Ive tried the same with static library which works find:
g++ -static -c sha.cpp -o libsha.o
ar rcs libsha.a libsha.o
g++ main.cpp -o main -L. -lsha
The platform is cygwin on windows and here is the output:
rob#pc /cygdrive/c/src/a
$ g++ main.cpp -o shatest -L. -lsha
/usr/lib/gcc/i686-pc-cygwin/4.3.4/../../../../i686-pc-cygwin/bin/ld: cannot find -lsha
collect2: ld returned 1 exit status
Ive read all forum posts but the library is in the same folder!
$ ls
libsha.so main.cpp sha.cpp sha.h sha.o
The reason im doing this is that on another platform, a library is being created whereby when one object is called, it works, but the app crashes when the second object is being constructed. Im doing the above as a simple test! (annoyingly not so simple).
source files below:
main.cpp
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include "sha.h"
void *thread_one( void *ptr );
void *thread_two( void *ptr );
main()
{
pthread_t thread1, thread2;
int iret1, iret2;
/* Create independent threads each of which will execute function */
iret1 = pthread_create( &thread1, NULL, thread_one, 0);
iret2 = pthread_create( &thread2, NULL, thread_two, 0);
/* Wait till threads are complete before main continues. Unless we */
/* wait we run the risk of executing an exit which will terminate */
/* the process and all threads before the threads have completed. */
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
printf("Thread 1 returns: %d\n",iret1);
printf("Thread 2 returns: %d\n",iret2);
exit(0);
}
void *thread_one( void *ptr )
{
printf("Run thread_one\n");
CObj1 obj;
}
void *thread_two( void *ptr )
{
printf("Run thread_two\n");
CObj2 obj;
}
sha.cpp
#include <stdio.h>
#include <stdlib.h>
#include "sha.h"
CObj1::CObj1()
{
printf("CObj1\n");
a = 10;
printf("CObj1: %d \n", a);
}
CObj2::CObj2()
{
printf("CObj2\n");
a = 10;
printf("CObj2: %d \n", a);
}
sha.h
#ifndef LIB
#define LIB
class CObj1
{
public:
CObj1();
private:
int a;
};
class CObj2
{
public:
CObj2();
private:
int a;
};
#endif